Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

Route data vs. Model data – who wins?

Data model route vs Wins
0
Posted

Route data vs. Model data – who wins?

0

Q: If we have in route defined parameter with same name as property at model and then we call it from strongly typed HTML helper (for example Html.TexBoxFor(x => x.PropertyName)), we get value from route parameter instead of property of model. A: This is actually the correct behavior. MVC has a concept called ModelState (accessible via the Controller.ModelState property) which contains information about the model the MVC request is currently working with. The ModelState collection stores raw values submitted by the user and a list of validation errors for each of those values. For a complete example see http://forums.asp.net/p/1559541/3846809.aspx When displaying a form, all HTML helpers look in ModelState first to get the value that should be displayed to the user, then they look at the actual model itself if ModelState doesn’t contain anything useful. The reason for this is so that user input isn’t blasted away in the event of a conversion error. For example, say that the user types

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123