1

I am trying to get the model binder to recognize the change. I am not sure what I am missing here. Basically the initial page population pulls the page number from the database. I then have the sortable working, the raw HTML in Firebug shows the change in order. But when I post back to the model first off it is not figuring out to go the post method and the other issue is Survey. Pages does not seem to have the change in order.

View

 @for (var i = 0; i < Model.Pages.Count; i++)
                    {
                        var page = Model.Pages.ElementAt(i);

                        @Html.Hidden("Pages[" + i + "].PageId", page.PageId, new { @class = "page_index" })
                        @Html.Hidden("Pages[" + i + "].PageNumber", page.PageNumber)

                        <li id="@page.PageId" class="sortable-item text-center ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s">
                        </span>@page.PageNumber</li>
                    }

                </ul>

JavaScript

<script type="text/javascript">
    $(document).ready(function () {
        $('.sortable').sortable({
            stop: function (event, ui) {
                 var formData = $('#editSurveryForm').serialize();
                 $.ajax({
                    url: "@Url.Action("Edit")",
                    data: formData,
                    type: 'POST',
                    traditional: true,
                    success: function () {
                        alert("success");
                    },
                    error: function () {
                        alert("fail");
                    }
                }
                );
            }
        });
    });
</script>

Controller

    [HttpPost]
    public ActionResult Edit(Survey survey)
    {
        if (!ModelState.IsValid)
        {
            return View("EditSurvey", survey);
        }

        surveyRepository.UpdateSurvey(survey);

        return RedirectToAction("Index", "Administration");
    }

1 Answer 1

1

Ok, I figure this one out. I had the hidden fields outside of the <li></li> tag. Once I moved them inside I did all my logic as I would have.

    <div class="span9">
        <div class="span4">
            <ul class="sortable_page_list">
                @for (var i = 0; i < Model.Pages.Count; i++)
                {
                    <li class="sortable-item text-center ui-state-default">
                        @Html.HiddenFor(model => model.Pages[i].PageId)
                        @Html.HiddenFor(model => model.Pages[i].PageNumber)
                        <span class="ui-icon ui-icon-arrowthick-2-n-s"></span>@Model.Pages[i].PageNumber
                    </li>
                }

            </ul>
            <div class="span1 pull-right internal-wrapper">
                @Html.ActionLink("Add", "AddPage", new { id = Model.SurveyId }, new { @class = "add_survey_icon common_icon_settings" })
            </div>
        </div>
    </div>

<script type="text/javascript">
$(document).ready(function () {
    $('.sortable_page_list').sortable({

        update: function (event, ui) {
            var counter = 1;
            $("[id*='PageNumber']").each(function() {
                $(this).attr("value", counter);
                counter++;
            });

            //var surveyToUpdate = $('#editSurveyForm');
            $.ajax({
                url: '@Url.Action("Edit","Administration")',
                contentType: "application/json; charset=utf-8",
                data: $('#editSurveyForm').serialize(),
                type: 'POST'
            });
        }
    });

});

Last thing to figure out is why the ajax post is not posting to the Post method with the survey form data

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.