1
我面臨一個問題,當我有一個複雜的模型時,如果我提交表單,它不會給我所有模型屬性的所有值,在下面的示例中,我我沒有得到回gridModel屬性:MVC在表單發帖時提交多個對象在模型中
型號
public class InventoryModel {
public GridModel GridModel { get; set; }
public Int32 UserKey { get; set; }
}
public class GridModel {
public String GridId { get; set; }
public String GridName { get; set; }
public List<String> columns { get; set; }
}
控制器
public ActionResult Index(){
InventoryModel model = new InventoryModel();
model.UserKey= 20014;
model.GridModel = new GridModel();
model.GridModel.GridId = "jqgInventory";
model.GridModel.GridName = "Inventory Grid";
return View(model);
}
[HttpPost]
public ActionResult Index(InventoryModel model){
Int32 userId = model.UserKey; // This has a value
String gridId = model.GridModel.GridId; // This doesn't have a value
String gridName= model.GridModel.GridName; // This doesn't have a value
}
查看
@model InventoryModel
@using (Html.BeginForm()) {
@Html.TextBoxFor(m => m.UserKey, new { @class = "w200" })
@Html.TextBoxFor(m => m.GridModel.GridId , new { @class = "w200" })
@Html.TextBoxFor(m => m.GridModel.GridName, new { @class = "w200" })
<input type="submit" value="Submit" />
}
任何建議請將不勝感激。
感謝, 阿拉
不好意思啊這是MVC4 –
研究建立一個自定義的模型綁定模型映射到您的視圖模型。 –