我有一个带有自定义对象的视图模型。在最初的获取中,我填充了 Foo 并使用了 Foo 的几个属性。
在帖子中,我发现视图模型上的 Foo 为空。
我可以添加到我的视图中,@Html.HiddenFor(x => x.Foo.Id)
这可以确保 Foo 至少填充了一个 Id,但是我可能需要为所有属性添加类似的代码。
有没有办法发回完整的对象?
public class RequestModel
{
public Foo Foo{ get; set; }
[Required]
[Display(Name = "Comment")]
public string Comment { get; set; }
}
控制器
public ActionResult Index(int? id)
{
//Populate Foo here using EF and add it to the model
var model = new RequestModel { Foo = foo };
return View(model);
}
[HttpPost]
public ActionResult Index(int? id, RequestModel model)
{
return View(model);
}
看法
@Html.DisplayTextFor(m=>m.Application.Name)
ETC