Sortable, draggable, orderable table + mvc 没有正确更新顺序

Sortable, draggable, orderable table + mvc not updating order correctly

我正在尝试向我的视图中添加一个可订购的 table。排序在视图 New.cshtml 中进行时有效。它使用 jQuerys sortable。如果我添加一些指令(在本例中为 1-5),我可以移动它们,这要归功于 sortable 脚本。但是当我尝试在重新排序后添加一条新指令时,顺序从:

估计跟隐藏形式有关系

@Html.HiddenFor(m => m.Recipe.Instructions[i].Number, new { @id = "Number"})

当我重新排序 sor 中的字段时,它需要更新它的值table table。

我在这里错过了什么?

RecipeController.cs

public ActionResult Create(NewRecipeViewModel viewModel, string command)
{

//...more code...

    var instructionList = new List<Instruction>();

    if (viewModel.Recipe.Instructions != null)
    {
        for (int i = 0; i < viewModel.Recipe.Instructions.Count; i++)
        {
            var instruction = new Instruction
            {
                Id = viewModel.Recipe.Instructions[i].Id,
                Name = viewModel.Recipe.Instructions[i].Name,
                Number = viewModel.Recipe.Instructions[i].Number

            };
            instructionList.Add(instruction);
        }
    }

    if (command.Equals("AddInstruction"))
    {
        var instruction = new Instruction
        {
            Name = viewModel.NewInstruction.Name,
            Number = viewModel.Recipe.Instructions.Count + 1
        };

        recipe.Instructions.Add(instruction);
        viewModel.Recipe = recipe;

        return View("New", viewModel);
    }

//...more code...
}

New.cshtml

<div class="form-group" style="margin-top: 170px;">
    <label class="col-md-2 control-label">
        Lägg till instruktion:
    </label>
    <div class="col-md-10">
        @Html.TextBoxFor(m => m.NewInstruction.Name, new { @class = "form-control" })
    </div>
</div>
<div class="form-group" style="margin-top: 300px;">
    <label class="col-md-2 control-label">
    </label>
    <div class="col-md-10">
        <button type="submit" name="command" value="AddInstruction" class="btn btn-primary">Add instruction</button>
    </div>
</div>
<div class="form-group" style="margin-top: 100px;">

    <label class="col-md-2 control-label">
        Instructions:
    </label>
    <div class="col-md-10">
        <table class="table table-bordered table-hover pagin-table" style="margin-top: 10px">
            <thead>
                <tr bgcolor="#f5f5f5">
                    <th>Order:</th>
                    <th>Instruction:</th>
                </tr>
            </thead>
            <tbody id="sortable">
                @if (Model.Recipe.Instructions != null)
                {
                    for (int i = 0; i < Model.Recipe.Instructions.Count; i++)
                    {
                        <tr>
                            @Html.HiddenFor(m => Model.Recipe.Instructions[i].Id)
                            <td class="order">
                                @Model.Recipe.Instructions[i].Number
                                @Html.HiddenFor(m => m.Recipe.Instructions[i].Number, new { @id = "Number"})
                            </td>
                            <td>
                                @Model.Recipe.Instructions[i].Name
                                @Html.HiddenFor(m => m.Recipe.Instructions[i].Name)
                            </td>
                        </tr>
                    }
                }
            </tbody>
        </table>
    </div>
</div>
 <script type="text/javascript">
     $(document).ready(function() {
             $('#sortable').sortable({
                 update : function(event, ui) { 
                     $('td.order').each(function(index) {
                        var order = index + 1;
                        $(this).find('span').text(order);
                        $(this).find('.Number').val(order);
                     });
                 }
             });
     });
 </script

编辑: 已添加

instructionList.Sort((s1, s2) => s1.Number.CompareTo(s2.Number));

到RecipeController.cs。其余的归功于 Stephen Muecke。

RecipeController.cs

public ActionResult Create(NewRecipeViewModel viewModel, string command)
{

//...more code...

    var instructionList = new List<Instruction>();

    if (viewModel.Recipe.Instructions != null)
    {
        for (int i = 0; i < viewModel.Recipe.Instructions.Count; i++)
        {
            var instruction = new Instruction
            {
                Id = viewModel.Recipe.Instructions[i].Id,
                Name = viewModel.Recipe.Instructions[i].Name,
                Number = viewModel.Recipe.Instructions[i].Number

            };
            instructionList.Add(instruction);
        }
        instructionList.Sort((s1, s2) => s1.Number.CompareTo(s2.Number));
    }

//...more code...
}

New.cshtml

 @* More code *@
 <td class="order">
      <span>@Model.Recipe.Instructions[i].Number</span>
      @Html.HiddenFor(m => m.Recipe.Instructions[i].Number, new {@class = "Number"})
 </td>

 @* More code *@    

 <script type="text/javascript">
     $(document).ready(function() {
             $('#sortable').sortable({
                 update : function(event, ui) { 
                     $('td.order').each(function(index) {
                        var order = index + 1;
                        $(this).find('span').text(order);
                        $(this).find('.Number').val(order);
                     });
                 }
             });
     });
 </script

在您的标记中,将 Model 更改为 'Html.HiddenFor()' HTML 助手中的投影变量 m

<tr>
    @Html.HiddenFor(m => m.Recipe.Instructions[i].Id)
    <td class="order">
        @Model.Recipe.Instructions[i].Number
        @Html.HiddenFor(m => m.Recipe.Instructions[i].Number, new { @id = "Number"})
    </td>
    <td>
        @Model.Recipe.Instructions[i].Name
        @Html.HiddenFor(m => m.Recipe.Instructions[i].Name)
    </td>
</tr>

您在 update 函数中的选择器不正确,将 return undefined (您缺少 # (id 选择器),无论如何 $ 创建一个 jQuery 对象,所以它将是 .val(...),而不是 value=....

但是使用 $('#Number').val(index + 1) 将无法正常工作,因为它只会更新具有 id="Number". Duplicateid` 属性的第一个元素无效 html.

改用 class 名称和相对选择器。将 html 更改为

<td class="order">
    <span>@Model.Recipe.Instructions[i].Number</span>
    @Html.HiddenFor(m => m.Recipe.Instructions[i].Number, new { @class = "Number"})
</td>

然后脚本到

$('#sortable').sortable({
    update : function(event, ui) { 
        $('td.order').each(function(index) {
            var order = index + 1;
            $(this).find('span').text(order );
            $(this).find('.Number').val(order );
        });
    }
});