0
我有幾種形式,所有這些都需要每個省/州的複選框。因此,我已經做了部分視圖來呈現表單中的複選框以促進代碼重用。但是,當用戶將表單提交給控制器方法時,RegionsViewModel不會被綁定。總體問題是,我如何獲得多個表單來共享局部視圖和視圖模型?模型綁定綁定在多個級別
這裏是我的情況的樣本代碼 模型
public class Form1ViewModel
{
/* Some properties */
public RegionsViewModel Regions {set; get;}
}
public class Form2ViewModel
{
/* Some properties */
public RegionsViewModel Regions {set; get;}
}
public class Form3ViewModel
{
/* Some properties */
public RegionsViewModel Regions {set; get;}
}
public class RegionsViewModel
{
public bool ON {set; get;}
public bool QC {set; get;}
/* this continues for all provinces and states */
}
控制器
[HttpPost]
public ActionResult Form(Form1VewModel model) {
//All properties except for model.RegionViewModel does not bind properly to the submitted form :(
}
Form1ViewModel.aspx
<% using (Html.BeginForm())
{%>
<!-- Binds some property -->
<% Html.RenderPartial("Controls/RegionSelector", Model.Regions); %>
<input type="submit" value="Submit Form!" />
<%}%>
控制/ RegionSelector.ascx
<%=Html.CheckBoxFor(x => x.AvailableProvince_ON> ON
<%=Html.CheckBoxFor(x => x.AvailableProvince_QC> QC
<!-- Binds to all provinces and states -->
更新 用「Model.Region」替換「Model.RegionSelectorVm」。感謝您在我的演示代碼Darin Dimitrov中查找錯誤。