0

If my Model is Contacts then I can easily get it in controller like this:

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

But if my Model is wrapper for Contacts and something else then in View I display it using Model.contact.

How to get Contact in Controller the same way like I did in first case? I don't want to use Formcollection.

ilija veselica
  • 9,414
  • 39
  • 93
  • 147
  • 1
    You're going to need to show us more code, its not exactly clear what you want. Can you give an example of a controller action, and a view.... – Matthew Abbott Jun 30 '10 at 09:29

2 Answers2

2

If you want to bind only the Contact but it is not the Model of your view but it is part of it as you have written, you can do the following for create:

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

And for the edit you can do the same, and you need to specify also in UpdateModel the prefix, like this:

    [HttpPost]
    public ActionResult Edit([Bind(Prefix = "Contact")]Contact contact){
      UpdateModel(contact, "Contact");
    }
apolka
  • 1,711
  • 4
  • 16
  • 23
1

For example you have

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

You can get it back by using the same type object as parameter:

[HttpPost]
public ActionResult Create(MyViewModel returnedModel)
{
    Contact returnedContact = returnedModel.MyContact;
    // ...
}
Yakimych
  • 17,612
  • 7
  • 52
  • 69