0

I have a queryparameter named "from" (a from date) and also a input which binds to a property named From

In my ViewModel contructor i set the From property to a date...

This works if the query parameter name and the property name are different, but if they are same MVC3 with some magic takes whatever value there are in the query param and binds against that, it does not care what value are in the From property.. Why? this atuomagic is so wrong on so many levels! How do I disable it?

edit: It doesnt matter what value the Property gets, if a querystring exists with the same id as the input MVC automatic takes that value and assign it to the input element

2
  • How do you set the property ? Commented Oct 19, 2011 at 10:45
  • I am having exactly the same issue. + 1 for everyone that was negative here since I believe every information here was useful. Commented Sep 9, 2013 at 11:12

2 Answers 2

2

You need to call

ModelState.Clear();

Before returning from your controller action.

The issue is that the ModelState has the value from the query string, and that takes precedence over the value in your model when binding occurs.

Whether this is a bug or a feature depends on your point of view... http://blogs.msdn.com/b/simonince/archive/2010/05/05/asp-net-mvc-s-html-helpers-render-the-wrong-value.aspx

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

Comments

0

MVC works by convention, and binds values to the model by names. To understand what is happening, I suggest you read this blog article from Phil Haack: What’s the Difference Between a Value Provider and Model Binder?

It is bad practice to have two unrelated elements with identical names in the same request, as a name collision is very likely to cause unexpected problems. Best practice is to rename one of these elements so that you eliminate the name collision.

2 Comments

Why the downvote? The answer is correct, appropriate and informative.
they are not unrelated, I have a query string "from" which relates to the property From on the model, which is binded to a field in the view. The problem is that MVC overrieds my model and binds the query string directly to the view..

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.