代码之家  ›  专栏  ›  技术社区  ›  Joel Wiklund

可排序、可拖动、可订购表+mvc未正确更新订单

  •  2
  • Joel Wiklund  · 技术社区  · 8 年前

    jQuerys sortable . 如果我添加了一些指令(本例中是1-5条指令),我可以通过可排序脚本来移动它们。但当我尝试在重新排序后添加新指令时,顺序从:

    enter image description here

    到

    enter image description here

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

    当我对可排序表中的字段重新排序时,它需要更新它的值。

    我错过了什么?

    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...
    }
    

    新建.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公司

    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...
    }
    

    新建.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
    
    2 回复  |  直到 8 年前
        1
  •  1
  •   user3559349 user3559349    8 年前

    您的选择器在 update 函数不正确,将返回 undefined # $ 创建一个jQuery对象,因此 .val(...) value=... .

    $('#Number').val(index + 1) 将无法正常工作,因为它只会用 id="Number". Duplicate

    改用类名和相对选择器。将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 );
            });
        }
    });
    
        2
  •  0
  •   akerra    8 年前

    在标记中,更改 Model 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>
    
    推荐文章