Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different models for Get and Post - MVC

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" />
}
like image 256
Rasmus Avatar asked Oct 11 '11 12:10

Rasmus


People also ask

What is GET and POST action types in MVC?

Both GET and POST method is used to transfer data from client to server in HTTP protocol but the Main difference between the POST and GET method is that GET carries request parameter appended in URL string while POST carries request parameter in message body which makes it more secure way of transferring data from ...

How many models are there in MVC?

In fact, in ASP.NET MVC, there are three distinct types of model: the domain model, view model and input model.

Can we use two multiple models with a single view?

You can use multiple models in a single view by creating a common model for all the models that are to be used in a single view. To achieve this, refer to the following steps. First, create a new model (common for all models) and refer all other models that are to be used in the same view.


2 Answers

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; }
}
like image 132
Trax72 Avatar answered Oct 19 '22 22:10

Trax72


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...

like image 29
Robert Koritnik Avatar answered Oct 19 '22 23:10

Robert Koritnik