我有这样的视图模型视图模型属性绑定到空
public class MyViewModel
{
public Customer Customer{ get; set; }
public Account Account{ get; set; }
public DateTime MyDate{ get; set; }
}
此视图
@using (Html.BeginForm("Final", "Home", FormMethod.Post, new { @class = "form" }))
{
@Html.AntiForgeryToken()
@Html.HiddenFor(m => m.Customer)....
@Html.DisplayFor(m => m.Customer.FirstName) //This displays the name ok
这是我的控制器
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Final(MyViewModel viewModel)
{
viewModel.Customer.... //This is null
的问题是,我不有编辑属性,只显示它们,然后保存到数据库,这就是为什么我使用HiddenFor,但属性Customer和Account bind t o null。 问题是什么?也许是隐藏的?
PS:我有我进行相应的回报查看GET方法(视图模型)
这是我通过视图模型
[HttpGet]
public ActionResult Final()
{
var viewModel = new MyViewModel
{
//set the properties, etc
};
return View(viewModel);
}
你从哪里通过模型 – jamiedanq
视图的模型是@model ... MyViewModel – user70014
似乎你不想要求数据库两次。但是只保留Hidden内部的客户ID会容易得多。或者您可以将Customer对象保留在TempData中。 – Berkay