我正在尝试向我的视图添加可排序的表。在View New.cshtml中进行排序时,可以进行排序。它使用jQuerys sortable。如果我添加了一些说明(在这种情况下为1-5),那么由于可排序的脚本,我可以移动它们。但是,当我尝试在重新排序后添加新指令时,其顺序从:
到
我想这与隐藏表单有关
@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...
}
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。其余的归功于斯蒂芬·穆克。
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
答案 0 :(得分:1)
您在update
函数中的选择器不正确,将返回undefined
(您缺少#
(id选择器),并且在任何情况下,$
都会创建一个jQuery对象,因此它将是.val(...)
,而不是value=...
。
但是使用$('#Number').val(index + 1)
不能正常工作,因为它只会更新具有id="Number". Duplicate
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 );
});
}
});
答案 1 :(得分:0)
在您的标记中,将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>