2011-04-04 69 views
1

我正在嘗試顯示動態複選框的列表並允許用戶選擇一個或多個。一旦返回到控制器,我將需要採取所有檢查的ID,並寫入每個數據庫的記錄。在MVC2中發佈checKbox值

以下是與此相關的代碼片段。

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); 
    } 
} 

回答

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