Asp.net mvc2 - how to get model and model.something in the same way in the controller?

If mine Modelis Contacts, then I can easily get it in the controller as follows:

[HttpPost]
public ActionResult Create(Contact contact)
...

But if my Modelis a wrapper for Contactsand something else, and then in View, I show it with Model.contact.

How to get a contact in the controller in the same way as in the first case? I do not want to use Formcollection.

0
source share
3 answers

If you want to bind only the Contact, but this is not the Model of your presentation, but it is part of it, as you wrote, you can do the following to create:

[HttpPost]
public ActionResult Create([Bind(Prefix = "Contact")]Contact contact)

, UpdateModel , :

    [HttpPost]
    public ActionResult Edit([Bind(Prefix = "Contact")]Contact contact){
      UpdateModel(contact, "Contact");
    }
+2

,

public class MyViewModel
{
    Contact MyContact { get; set; }
    Object SomethingElse { get; set; }
}

, , :

[HttpPost]
public ActionResult Create(MyViewModel returnedModel)
{
    Contact returnedContact = returnedModel.MyContact;
    // ...
}
+1

You can use model bindings: there are already some answers to stackoverflow that: ASP.NET MVC2 - Examples of model bindings

0
source

Source: https://habr.com/ru/post/1757915/


All Articles