views:

26

answers:

2

Hello, I have an user view model that has the following properties:

 public User user;
    public List<Language> Languages;

I send the above model to the view and use html helpers to build the form, so I end up with something like:

<form action="/Users/Edit/5" method="post"><input id="user_UserId" name="user.UserId" type="hidden" value="5" />
First Name
Last Name
Email
Language
- en en

Now, I try to read the POST in something that initially was something like :

[AcceptVerbs( HttpVerbs.Post )]

public ActionResult Edit( int UserId, FormCollection form ) {

and cannot get the user.UserId variable, user.FirstName variable etc.

Any idea what needs to be done to be able to read this kind of POST request. I'm kind of reluctant to modifying my ViewModel as it is very simple and easy to maintain as it is.

Thank you.

+2  A: 

I had a similar problem some time ago. This article was helpful: Editing a variable length list

I don't seem to understand how to apply the above specified post to my specific case. Can I perhaps alter the name of the resulted input elements? I'm using the HiddenFor helper for the UserId.
Interfector
Notice the "using(Html.BeginCollectionItem("gifts")". The trick is to surround your collection with this Helper Class. That's the most important part of the article.
A: 

Apparently the easy answer is to use Prefix, something like:

public ActionResult Edit([Bind(Prefix="user")] int UserId, FormCollection form ) { }

However, I'm still getting the

The parameters dictionary contains a null entry for parameter 'UserId' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Edit(Int32, System.Web.Mvc.FormCollection)'

Any idea how to fix that?

Interfector