Get 和 Post 的不同模型 - MVC
据我从下面的问题中了解到,应该可以使用不同的模型来执行“获取”和“发布”操作。但不知怎的,我没能实现这一目标。
我缺少什么?
相关问题:在控制器操作中使用两个不同的模型对于 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" />
}
As I understand from the question below it should be possible to use different models for Get and Post actions. But somehow I'm failing to achieve just that.
What am I missing?
Related question: Using two different Models in controller action for POST and GET
Model
public class GetModel
{
public string FullName;
public string Name;
public int Id;
}
public class PostModel
{
public string Name;
public int Id;
}
Controller
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();
}
}
View
@model MvcApplication1.Models.GetModel
@using (Html.BeginForm()) {
@Html.EditorFor(x => x.Id)
@Html.EditorFor(x=>x.Name)
<input type="submit" value="Save" />
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的模型没有使用正确的访问器,因此模型绑定不起作用。将它们更改为这样,它应该可以工作:
Your models aren't using proper accessors so model binding doesn't work. Change them to this and it should work:
稍微澄清一下,
GET 和 POST 控制器操作可以轻松使用它们需要的任何类型。实际上我们这里讨论的不是模型。模型是表示某些应用程序状态/数据的一组类/类型。因此应用程序或数据模型。
我们在这里处理的是:
所以您的应用程序模型仍然是相同的。
GetModel
和PostModel
只是该模型中的两个类/类型。它们本身不是模型。不同类型?我们当然可以!
在您的情况下,您使用视图模型类型
GetModel
,然后将其数据传递给PostModel
操作参数。由于这两个类/类型都具有具有相同匹配名称的属性,因此默认模型绑定器将能够填充PostModel
属性。如果属性名称不相同,则必须更改视图以重命名输入以反映 POST 操作类型属性名称。您也可以拥有一个
GetModel
类型的视图,然后使用几个不同的参数发布操作,例如:或其他任何内容。您只需确保发布数据可以绑定到这些参数及其类型属性......
A bit of clarification
GET and POST controller actions can easily use whatever types they need to. Actually we're not talking about models here. Model is a set of classes/types that represent some application state/data. Hence application or data model.
What we're dealing here are:
So your application model is still the same. And
GetModel
andPostModel
are just two classes/types in this model. They're not model per-se.Different types? Of course we can!
In your case you're using a view model type
GetModel
and then pass its data toPostModel
action parameter. Since these two classes/types both have properties with same matching names, default model binder will be able to populatePostModel
properties. If property names wouldn't be the same, you'd have to change the view to rename inputs to reflect POST action type property names.You could as well have a view with
GetModel
type and then post action with several different prameters like:Or anything else. You'll just have to make sure that post data can be bound to these parameters and their type properties...