Get 和 Post 的不同模型 - MVC

发布于 2024-12-09 05:57:59 字数 1142 浏览 3 评论 0原文

据我从下面的问题中了解到,应该可以使用不同的模型来执行“获取”和“发布”操作。但不知怎的,我没能实现这一目标。

我缺少什么?

相关问题:在控制器操作中使用两个不同的模型对于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瑾兮 2024-12-16 05:57:59

您的模型没有使用正确的访问器,因此模型绑定不起作用。将它们更改为这样,它应该可以工作:

public class GetModel
{
   public string FullName { get; set; }
   public string Name { get; set; }
   public int Id { get; set; }
}

public class PostModel
{
   public string Name { get; set; }
   public int Id { get; set; }
}

Your models aren't using proper accessors so model binding doesn't work. Change them to this and it should work:

public class GetModel
{
   public string FullName { get; set; }
   public string Name { get; set; }
   public int Id { get; set; }
}

public class PostModel
{
   public string Name { get; set; }
   public int Id { get; set; }
}
櫻之舞 2024-12-16 05:57:59

稍微澄清一下,

GET 和 POST 控制器操作可以轻松使用它们需要的任何类型。实际上我们这里讨论的不是模型。模型是表示某些应用程序状态/数据的一组类/类型。因此应用程序数据模型。

我们在这里处理的是:

  • 视图模型类型
  • 操作方法参数类型

所以您的应用程序模型仍然是相同的。 GetModelPostModel 只是该模型中的两个类/类型。它们本身不是模型。

不同类型?我们当然可以!

在您的情况下,您使用视图模型类型 GetModel ,然后将其数据传递给 PostModel 操作参数。由于这两个类/类型都具有具有相同匹配名称的属性,因此默认模型绑定器将能够填充 PostModel 属性。如果属性名称不相同,则必须更改视图以重命名输入以反映 POST 操作类型属性名称。

您也可以拥有一个 GetModel 类型的视图,然后使用几个不同的参数发布操作,例如:

public ActionResult Edit(Person person, IList<Address> addresses)
{
    ...
}

或其他任何内容。您只需确保发布数据可以绑定到这些参数及其类型属性......

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:

  • view model types
  • action method parameter types

So your application model is still the same. And GetModel and PostModel 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 to PostModel action parameter. Since these two classes/types both have properties with same matching names, default model binder will be able to populate PostModel 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:

public ActionResult Edit(Person person, IList<Address> addresses)
{
    ...
}

Or anything else. You'll just have to make sure that post data can be bound to these parameters and their type properties...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文