c# - 可排序、可拖动、可排序表 + mvc 未正确更新顺序

标签 c# asp.net-mvc jquery-ui-sortable draggable

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

enter image description here

enter image description here

我猜和隐藏形式有关系

@Html.HiddenFor(m => m.Recipe.Instructions[i].Number, new { @id = "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);
        }
    }

    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。其余的归功于 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...
}

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

最佳答案

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

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

改用类名和相对选择器。将 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 );
        });
    }
});

关于c# - 可排序、可拖动、可排序表 + mvc 未正确更新顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52597650/

相关文章:

c# - 如何在类似于 phpMyAdmin 的 asp.net 中创建动态表/字段

c# - 具有多个 STA 线程的 Wpf 应用程序仍然阻塞用户界面

c# - 在 mvc4 中捕获未处理的异常

jquery - Portlet 仅在 IE7 中正确关闭

javascript - 拖动元素时,如何获取要替换的元素的位置?

c# - 使用接口(interface)的隐式运算符

c# - 将 appsettings 配置 DI 添加到 HostBuilder 控制台应用程序

javascript - User.Identity.Name 在 Safari 浏览器上为空

c# - 每个 HTTP 请求多少个 SQL 查询是最佳的?

javascript - jquery ui 可排序 - 没有悬停事件