我遇到一个问题,当我有一个复杂的模型时,如果我提交表单它不会给我所有模型属性的所有值,在下面的例子中,我没有得到gridModel属性:< / p>
模型
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" />
}
任何建议都将不胜感激。
谢谢, 阿拉
答案 0 :(得分:3)
您可以使用ViewModel而不是实际的Model。这将是一个更平坦的类,它反映了View专门的数据。
public class InventoryViewModel{
Int32 UserKey {get; set; }
public String GridId { get; set; }
public String GridName { get; set; }
}
如有必要,您的控制器可以将您的模型映射到ViewModel