0

When I pass some properties through querystring in url, for instance ..CustomAction?something=10 this value is passe to the view fields.

Suppose some simple POCO Class is simple like this:

class MyClass {
    public int something {get;set;}
}

whenever or not I set the "somethig" value in controller before calling the view, for instance.

public ActionResult CustomAction(int something){
    var entity = new MyClass() { something = 2};
    return view(entity);
}

My view always shows the URL parameter 10 instead of 2

@model MyClass
@Html.EditorFor(model => Model.something)

If the URL is .../CustomAction the value in the "editorFor" is set to 2 (as expected)

If the URL is .../CustomAction?something=10 the value in the URL (10) overrides the value in the model (2) and the "editorFor" field value is set to 10.

How can I avoid this parameter override?

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
Daniel Santos
  • 14,328
  • 21
  • 91
  • 174
  • Show the signature of the method. You appear to have your model as a parameter of the method and therefore the value is being added to `ModelState` (so you would need to clear it) –  Oct 12 '16 at 21:23
  • @StephenMuecke edited on the question – Daniel Santos Oct 12 '16 at 21:52
  • _My view always shows the URL parameter 10 instead of 2_ - of course it does - the code in that method changes the value of a property, not the value of the url. And add`ModelState.Clear();` before you return the view (refer [this answer](http://stackoverflow.com/questions/26654862/textboxfor-displaying-initial-value-not-the-value-updated-from-code/26664111#26664111) for an explanation of the behavior) –  Oct 12 '16 at 22:06
  • 1
    Call ModelState.Clear() in your controller or simply change your property value/ parameter name different – Nathan Oct 12 '16 at 22:16

0 Answers0