我有一個包含list nad過濾器選項的視圖。我需要添加選項來一次更新列表中的選定項目。查看與列表和過濾選項看起來短這樣的:如何將值從複選框傳遞給控制器?
@model GWeb.Models.FilterModel
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Search criteria</legend>
@Html.LabelFor(model => model.ProjectId, "Project")
@Html.DropDownList("ProjectId",
new SelectList(ViewBag.projectListDesciption as System.Collections.IEnumerable, "Id", "Desciption"), "all")
@Html.LabelFor(model => model.StartWork, "From Date")
@(Html.Telerik().DatePicker()
.Name("StartWork")
.Value(Model.StartWork))
<input type="submit" value="Filter" name="submitButton"/>
</fieldset>
<fieldset>
<legend>Status change</legend>
Change status on selected:
@Html.DropDownList("Status",
new SelectList(ViewBag.statusList as System.Collections.IEnumerable, "Id", "Description"))
<input type="submit" value="Update" name="submitButton"/>
</fieldset>
}
@{Html.RenderPartial("WorkList", Model.workList);}
凡列表局部視圖呈現在短期看起來像這樣:所使用的
@model IEnumerable<GWeb.Models.WorkModel>
@foreach (var item in Model)
{
<tr>
<td>
@Html.CheckBox("chb" + item.Id, item.Selected)
</td>
...
</tr>
}
這裏是模型:
public class FilterModel
{
public int ProjectId { get; set; }
public DateTime? StartWork { get; set; }
public int? Status { get; set; }
public List<WorkModel> workList { get; set; }
}
public class WorkModel
{
public int Id { get; set; }
...
public bool Selected { get; set; }
}
在控制器中,我正在檢查單擊了哪個按鈕。如果它是'更新',我想執行修改。但workFilter.workList
始終爲空。
public ActionResult WorkManager(FilterModel workFilter, string submitButton)
{
if (submitButton == "Update")
{
if (workFilter.workList != null)
{
//...
}
}
else
{
//filter
}
//...
return View(workFilter);
我該如何檢查控制器中的複選框是否被選中更新?
任何幫助非常感謝!
這將創造新的FilterModel - 但我不希望創建一個新的。我需要使用現有的和它的價值觀。 – Marta