根据下面的问题,我理解应该可以在Get和Post操作中使用不同的模型。但是我却没能成功实现它。
我错过了什么?
相关问题:在控制器动作中使用两个不同的模型进行POST和GET
模型
public class GetModel
{
public string FullName;
public string Name;
public int Id;
}
public class PostModel
{
public string Name;
public int Id;
}
控制器
public class HomeController : Controller
{
public ActionResult Edit()
{
return View(new GetModel {Id = 12, Name = "Olson", FullName = "Peggy Olson"});
}
[HttpPost]
public ActionResult Edit(PostModel postModel)
{
if(postModel.Name == null)
throw new Exception("PostModel was not filled correct");
return View();
}
}
视图
@model MvcApplication1.Models.GetModel
@using (Html.BeginForm()) {
@Html.EditorFor(x => x.Id)
@Html.EditorFor(x=>x.Name)
<input type="submit" value="Save" />
}