【问题标题】:How can I send two values with one DropDownList / send 3 values with 2 input form ASP.NET MVC如何用一个 DropDownList 发送两个值/用 2 个输入表单 ASP.NET MVC 发送 3 个值
【发布时间】:2020-05-20 01:27:18
【问题描述】:

我有两张桌子,一张书桌和一张书架桌。在 book 表中我有实体:

id   title      category
1   GoodNature  Science

而在我的书架桌上我有实体:

id  book_id category    bookshelf_name
1   1       Science      A11

bookshelf.cs(模型)

public partial class bookshelf

{   

 public int id { get; set; }
 public int book_id { get; set; }
 public string category { get; set; }
 public string bookshelf_name { get; set; }
 public List BookCollection { get; set; }

}

BookShelfController.cs(控制器)

public ActionResult Create()
{
  master_bookshelf bookShe = new master_bookshelf();
  bookShe.BookCollection = db.master_book.ToList<master_book>();
  return View(bookShe);
}

[HttpPost]
public ActionResult CreateShelf(master_bookshelf Shelf)
{
  db.master_bookshelf.Add(Shelf);
  db.SaveChanges();
  return RedirectToAction("Index", "BookShelf");
}

BookShelf.cshtml(视图)

...
<form class="form-horizontal" method="post" action="CreateShelf">
...
<td>Book Name</td>
<td>
@Html.DropDownListFor(model => model.book_id, new SelectList(Model.HadiahCollection, "id", "titile"), "Select"
, new { @class = "form-control select2", @style = "width: 100%;", @required = "required" })
</td> <tr>
<td>Name</td>
<td><input type="text" class="form-control" name="bookshelf_name" required></td>
</tr>
...

我如何通过简单地使用 2 个输入表单来填写书架表中的类别列,该表单是选择书名的下拉列表,然后将 id 发送到 book_id 和书架名称的文本输入表单?

【问题讨论】:

    标签: asp.net asp.net-mvc asp.net-mvc-4 asp.net-mvc-3


    【解决方案1】:

    如果您有来自选择列表选项的键 (book.id),请通过按 id 查询图书来查询类别。你在使用实体框架吗?

    【讨论】:

    • 当他们提交表单时,使用实体框架获取类别。类似于: bookshelf Shelf= context.bookshelf.Find(selected_bookshelf.id) 然后 category=shelf.category
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-13
    • 2021-06-07
    • 2022-01-11
    • 2019-06-26
    • 2016-07-13
    • 1970-01-01
    • 2016-04-25
    相关资源
    最近更新 更多