我正在尝试显示动态复选框列表,并允许用户选择一个或多个。一旦返回到控制器,我需要获取所有已检查的ID的ID,并将每个记录写入数据库。
以下是与此相关的代码片段。
DTO
public class OfficeVisitPartOfBodyDisplay
{
public int PartOfBodyId { get; set; }
public string PartOfBodyName { get; set; }
public bool PartOfBodyChecked { get; set; }
}
模型
public class OfficeVisitModel
{
public OfficeVisitEntity OfficeVisit { get; set; }
public TList<PartOfBodyEntity> PartOfBodies { get; set; }
public TList<OfficeVisitPartOfBodyEntity> OfficeVisitPartOfBodies { get; set; }
public List<OfficeVisitPartOfBodyDisplay> OfficeVisitPartOfBodyDisplays { get; set; }
public string PatientName { get; set; }
}
查看(相关部分)
<div data-role="fieldcontain">
<fieldset data-role="controlgroup">
<legend>Pain Area(s):</legend>
<% foreach (OfficeVisitPartOfBodyDisplay officeVisitPartOfBodyDisplay in Model.OfficeVisitPartOfBodyDisplays)
{ %>
<label for="partofbodydisplay<%= officeVisitPartOfBodyDisplay.PartOfBodyId %>"><%= officeVisitPartOfBodyDisplay.PartOfBodyName%></label>
<input type="checkbox" id="partofbodydisplay<%= officeVisitPartOfBodyDisplay.PartOfBodyId%>" name="partofbodydisplay" value="<%= officeVisitPartOfBodyDisplay.PartOfBodyName%>" />
<% } %>
</fieldset>
</div>
控制器,模型中的OfficeVisitPartOfBodyDisplays和partofbodydisplay总是返回时没有数据。
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult ActivePainArea(OfficeVisitModel model, OfficeVisitPartOfBodyDisplay[] partofbodydisplay, string submitButton)
{
switch (submitButton)
{
case "Save":
model.Message = "Save Coming Soon";
return View(model);
case "Cancel":
model.Message = "Cancel Coming Soon";
return View(model);
case "Complaint":
return RedirectToAction("ActiveComplaint", new { patientId = model.OfficeVisit.PatientId, readOnly = false });
case "Patient History":
model.Message = "Patient History Coming Soon";
return View(model);
//return RedirectToAction("ActivePatientHistory", new { patientId = model.OfficeVisit.PatientId });
default:
return View(model);
}
}
答案 0 :(得分:1)
尝试一下:
控制器:
public ActionResult ActivePainArea(OfficeVisitModel officeVisitPartOfBodyDisplay, string submitButton) {
查看:
<div data-role="fieldcontain">
<% using (Html.BeginForm()) { %>
<fieldset data-role="controlgroup">
<legend>Pain Area(s):</legend>
<% var i = 0; %>
<% foreach (var officeVisitPartOfBodyDisplay in Model.OfficeVisitPartOfBodyDisplays) { %>
<label for="partofbodydisplay<%= officeVisitPartOfBodyDisplay.PartOfBodyId %>">
<%= officeVisitPartOfBodyDisplay.PartOfBodyName%></label>
<%: Html.CheckBox("OfficeVisitPartOfBodyDisplays[" + i.ToString() + "].PartOfBodyChecked", officeVisitPartOfBodyDisplay.PartOfBodyChecked)%>
<% i++; %>
<% } %>
<input type="submit" value="save" />
</fieldset>
<% } %>
</div>
您也可以参考此页面以获取更多信息: Phil Haacked Model Binding To A List