0

I have spent all day trying to debug this and I need help. I am developing a JQuery Mobile application in ASP.Net MVC 4. I have a form with a hidden field on it, with a value that is derived from a model property:

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

After form submission, Model.ShowMsg is modified by the controller and then the form is redisplayed. The problem is that by the time the view gets to its javascript (DOM fully loaded), Model.ShowMsg does not show the modified value but still shows the initial value (before the post).

I found this as a possible solution, which describes my situation almost exactly, however clearing the ModelState in the controller does not solve the issue: http://blogs.msdn.com/b/simonince/archive/2010/05/05/asp-net-mvc-s-html-helpers-render-the-wrong-value.aspx

I understand how the modelstate retains the form field values so it can redisplay them in case of a validation error. I also understand that the HTML helpers in the view will use the ModelState first before using the model. Why do I still have the issue if I clear the modelstate?

I put a breakpoint in the controller and verified that the modelstate was cleared, however when the view is redisplayed after the postback, the following javascript code displays the old value:

<script>
    $(document).ready(function () {
        alert(document.getElementById("hdnShowMsg").value);
    });
</script>

How can this be?

Here is the html helper for the form:

@Using Html.BeginForm("Index", "NewDocument", Model, FormMethod.Post, New With {.id = "newdocument-form"})
4

0 回答 0