1

I'm fairly new to MVC and starting out with version 3. I'm having an issue with the jQuery reorder list that ships with MVC3.

The view renders from an enumeration of my model:

@using (Html.BeginForm())
{

    <ul id="questionlist" class="ReorderList">
        @for (int i = 0; i < data.Count(); i++)
        {
            <li>
                <span class="dragHandle"></span>
                @Html.HiddenFor(x => data[i].ID)
                @Html.HiddenFor(x => data[i].DisplayOrder)
                @Html.TextBoxFor(x => data[i].QuestionText)
            </li>
        }
    </ul>

    <input type="submit" value="Save" />
}

When the page loads jquery is init on the list:

<script type="text/javascript">
    $(document).ready(function () {
        var unordered = $("#questionlist").sortable({
            axis: 'y',
            update: listUpdated });
    });

    function listUpdated(event, ui) {
        $('input[name$="DisplayOrder"]').each(function (index) { this.value = index; });
    }
</script>

And as you can see after the order is updated jQuery is updating all the hidden fields that map to the DisplayOrder property on my model.

Debugging this I can see the values are posted back to the controller properly, business objects are updated and persisted in SQL Server correctly. Running a query on the table I can see everything is updated as it should be.

My problem occurs when the user clicks the 'Save' button the returned HTML is in the original order, and not the order they changed it to. Fiddler shows the HTML coming back in the original order, but the DisplayOrder hidden field has the correct(changed) values. Clicking a nav button on the screen to refresh the view renders the list in the correct order. I've debugged through the rendering process and everything appears to render in the correct order, I've also tried to decorate the controller with OutputCaching set to false, but of course, it didn't work since the DisplayOrder property was rendering with the correct values.

What am I doing wrong to make it show incorrectly the first postback, but not subsequent postbacks?

tereško
  • 58,060
  • 25
  • 98
  • 150
Jay
  • 6,224
  • 4
  • 20
  • 23
  • Right, I mean to say when the ActionResult returns the view, its just easier to say "postback". – Jay Feb 15 '11 at 18:56
  • Not finding any feedback I've decided to just output the html directly from the model (not indexed) and use jquery to build and post json updates to the controller, rather than have the MVC engine try to rebuild an object model from posted key value pairs. – Jay Mar 30 '11 at 18:21
  • Same issue but I'm looking for a less complex solution or I might simply forget about sorting records on server side during postback! – Hemant Tank Dec 13 '11 at 19:37

1 Answers1

0

Actually, I derive that it is NOT possible to alter the Model during the postback due to the processing model of HTML Helpers & MVC.

I might not have used the words correctly so better see it for yourself -

How to modify posted form data within controller action before sending to view?

Original link: http://www.gxclarke.org/2010/05/consumption-of-data-in-mvc2-views.html

Community
  • 1
  • 1
Hemant Tank
  • 1,724
  • 4
  • 28
  • 56