views:

74

answers:

3

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.

+1  A: 

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
+2  A: 

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
A: 

You could use model binders: there are already some answers on stackoverflow about that: ASP.NET MVC2 - Custom Model Binder Examples

Michel Tol