这里有很多关于如何将DropDownList选择值传递给ActionResult的好答案。但是,我无法找到有关传递所选DropDownList值和项目ID的任何信息。
为了说明,我正在处理一个表,其中每一行代表一个项目,唯一的用户可更改值是通过下拉列表和触发更新的按钮控制的状态。由于数据库中的状态列不可为空,因此预先选择下拉列表的值。当用户单击该按钮时,它应该发送有问题的项类型的ID以及用户想要为该项更改的状态,以便ActionResult可以正确地更新数据库
我当前的查看代码如下:
<div id="divGrid" style='position:absolute; width:auto; padding-right: 5%'>
<table id="checklistGrid" class="table table-bordered table-striped grid">
<tr>
<th>@Html.DisplayNameFor(model => model.ww)</th>
.... // more table headings
</tr>
@foreach (var ww in Model.GroupBy(x => x.ww))
{
<tr class="ww-header">
<td colspan="12"><span class="h2">@ww.Key</span></td>
</tr>
foreach (var manager in ww.GroupBy(x => x.manager))
{
<tr class="manager-header">
<td colspan="12"><span class="h4">@manager.Key</span></td>
</tr>
foreach (var item in manager)
{
<tr class="row-header">
<td>@Html.DisplayFor(modelItem => item.ww)</td>
.... //more columns
<td>
@* ------------- Area in question ------------- *@
@{
List<string> ddl = new List<string> { "Done", "Not Done", "N/A" };
SelectList sl = new SelectList(ddl, item.status);
using (Html.BeginForm("Update", "Checklist", new { id = item.ID, /* DDL Selected Value */}))
{
@Html.DropDownList("StatusDDL", sl);
<input type="submit" value="Update" />
}
}
@* --------------------------------------------- *@
</td>
<td>
@Html.DisplayFor(modelItem => item.applied_by)
</td>
@* [...] *@
控制器:
public ActionResult Update(int ID, string newStatus)
{
//// Get the model
//TaskInfo taskInfo = db.taskInfoSet.Where(m => m.ID == ID).FirstOrDefault();
//// Update properties
//taskInfo.status = newStatus;
//// Save and redirect
//db.Entry(taskInfo).State = EntityState.Modified;
//db.SaveChanges();
return RedirectToAction("Index");
}
鉴于以上内容,我如何对其进行编码,以便将选定的下拉值和item.ID都发送到ActionResult?
答案 0 :(得分:1)
@Html.DropDownList("StatusDDL", sl);
正在生成<select name="StatusDDL">
,其与您的方法参数string newStatus
的名称不匹配。使用
@Html.DropDownList("newStatus", sl)
或将方法更改为
public ActionResult Update(int ID, string StatusDDL)