tags:

views:

24

answers:

1

Not getting updates in MVC View following a post method

Hi,

Here's my controller code:

public ActionResult Search()
{
    ForecastManagementViewModel viewModel = new ForecastManagementViewModel();
    viewModel.JobDate = System.DateTime.Now;

    return View(viewModel);
}

[HttpPost]
public ActionResult Search(ForecastManagementViewModelPost model)
{
    ForecastManagementViewModel viewModel = new ForecastManagementViewModel();
    viewModel.JobDate = model.JobDate.AddDays(20);
    return View(viewModel);
}

The problem is that when the new view is returned it doesn't contain the updated date value. (By the way, there's a jquery EditorTemplate behind this DateTime type).

Here's a bit of the view code:

<% using (Html.BeginForm())
      { %>
<fieldset>

    <legend>Search Criteria</legend>

    <div class="divFirstColumn">
        <div><%= Html.LabelFor(m => m.JobDate) %></div>
        <%= Html.EditorFor(m => m.JobDate) %>
    </div>    

</fieldset>

 <% } %>   

Any ideas where I might be going wrong. I'm using MVC2 under Visual Studio 2008 SP1.

Cheers,

J Dubs.

A: 

Hey,

Could be a form mismatch issue... could you check the ID on the client side by doing view source to make sure it will properly hook up to the model when posting? Also, I would check, using a breakpoint in the post, the form collection to see what the key is on the server-side also. THe form collection is available as a property of the controller too.

Brian
Hi Brian, thanks for getting back so quickly. I've checked the markup and can confirm that the correct name is being used. I'll try to simplify the problem into a mini-project to see if there's perhaps some side-affects from the rest of the project. In the meantime, and other suggestions are welcome.
J Dubs
I've now created a test project and zipped it up. Can I upload this to stackoverflow? Perhaps in encoded form - it's a 13k zip that shows the problem clearly.
J Dubs
Found the answer at: http://blogs.msdn.com/b/simonince/archive/2010/05/05/asp-net-mvc-s-html-helpers-render-the-wrong-value.aspx
J Dubs
ModelState.Clear();
J Dubs
Ahh, makes sense.
Brian