Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Model value wrong in generated view

Ran into a very strange problem here using MVC3 and razor.

The app I'm putting together shows a series of interview questions. I have a view for a question (the content of the question comes from the viewmodel). So after I post back from one question it is possible I will be returning the same question view (but using a different viewmodel instance).

In the viewmodel I have a long property like so:

public long QuestionID { get; set; }

I was trying to use a hidden input field to store the value in the view:

@Html.HiddenFor(m => m.QuestionID)

My problem is that as I go from question to question the hidden field is not changing in the final generated html. It remains the value of the first questionID in all further questions. All the other content is changing just fine and I have a couple other hidden fields that are working fine. I have verified the correct values in the controller where the model is generated. I can set a breakpoint in the razor file and I can see the model is as it should be with the correct QuestionID. I tried using @Html.TextBoxFor as well and I have the same problem. When I manually make a hidden field like below it works fine, and this is what is really bothering me.

<input type="hidden" id="QuestionID" name="QuestionID" value="@Model.QuestionID" />

Any idea why this is happening?

like image 974
Adam Avatar asked Apr 30 '12 22:04

Adam


People also ask

How do you pass model data to view?

The other way of passing the data from Controller to View can be by passing an object of the model class to the View. Erase the code of ViewData and pass the object of model class in return view. Import the binding object of model class at the top of Index View and access the properties by @Model.

How does model update view in MVC?

In a web MVC the model does not update any view! The model should know nothing about the external world. Instead: (a) Controller updates the model and then reads the data from it, in order to pass it further to the view for displaying it; or (b) Controller updates the model.

Can we create view without model?

If you don't want to create View Model then it is completely fine. You can loose some benefit like automatic client side validation ( data annotation). It means you have to do all validation by your self in client side and later on server side.

What does @model represent in a razor view?

The @Model will contain all the data of the current page. So when you access a page on your site you can get data from that page with using the @Model.


1 Answers

After a form post, MVC keeps track of the posted values in ModelState. Even if you change the values of your model, the HiddenFor helpers will use the ModelState value first. This is due to the fact that binding may have failed, or binding may be a complex object and the string value in ModelState is what they actually entered.

You can solve this by using

ModelState.Remove("QuestionID");

or, if you really just want to treat it as a completely new page,

ModelState.Clear();
like image 92
bhamlin Avatar answered Sep 22 '22 07:09

bhamlin