1

我试图让模型绑定器识别更改。我不确定我在这里缺少什么。基本上,初始页面填充从数据库中提取页码。然后我有可排序的工作,Firebug 中的原始 HTML 按顺序显示更改。但是,当我首先发布回模型时,并没有想出发布方法,另一个问题是调查。页面顺序似乎没有变化。

看法

 @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>

控制器

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

        surveyRepository.UpdateSurvey(survey);

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

1 回答 1

1

好的,我想出了这个。<li></li>我在标签之外有隐藏的字段。一旦我把它们移到里面,我就按照我的逻辑做了所有的事情。

    <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'
            });
        }
    });

});

最后要弄清楚的是为什么 ajax 帖子没有使用调查表单数据发布到 Post 方法

于 2013-07-31T12:49:22.783 回答