5

I would like to know how exactly model binding works in ASP.NET MVC3. Since I am still waiting for my Professional ASP.NET MVC3 book and I cannot find anything by googling it, you are my last hope.

I know how to perform binding with simple objects but when it comes to ViewModels, especially with nested List<T>, I am unable to perform binding.

Thanks

Francesco

UPDATE:

For clarification, I mean Model binding from View to Action Methods, thanks

2
  • Why are you unable? You know you have to use @Model = etc etc? Well with a ViewModel it's just the same. But your ViewModel contains multiple data from various entities incomparison to a normal model which is usually a single entity (or collection of entities) Commented May 11, 2011 at 11:18
  • @Rhapsody: sorry, I meant binding from View to Action Method. Of course the other way around is quite straightforward. Thanks Commented May 11, 2011 at 11:50

2 Answers 2

0

The question is not completely clear, so I'll address what I think you are asking for help on.

In cases where a View Model entity has a property of List<T> or some other enumerable, it is not automatically bound to the resulting model instance that is available in the action method marked as HttpPost.

You simply need to to find a place to persist the data, or, simply re-query for it in your Action method and update the posted instance.

The most reliable way I have found involves serializing the data to JSON and putting those values into hidden form fields, but when I do this, my view models no longer have the List property, but rather, the serialized properties.

This dilemma usually forces me to re-evaluate the need for the data to be available on form posts, and in most cases it is because I have tried to reuse a view model across views that have different requirements.

Sign up to request clarification or add additional context in comments.

Comments

0

As far as I know there is no changes in model binding have been made in mvc3, so I suppose chapter about model binding from Pro ASP.NET MVC V2 Framework is still valid.

I recommend to use javascript when you have to bind nested lists to action parameter.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.