在“创建”页面上,我使用局部视图来动态添加文本框。这是实现:
创建.cshtml
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<table>
<tr>
<th colspan="3">Checks</th>
</tr>
<tbody id="tableCheck">
@Html.Partial("_checklist")
</tbody>
</table>
<input type="button" value="Add check" id="addCheck" />
}
脚本
$(function () {
$("#addCheck").click(function () {
$.ajax({
type: "POST",
url: '/Checklists/GenerateChecks',
data: $(this).closest("form").serialize(),
success: function (html) {
$('#tableCheck').html(html);
},
error: function (msg) {
console.log(msg);
}
});
});
});
好吧,部分视图本身 _checklist.cshtml,列表中的每个项目都有删除按钮。在按钮上,我试图在 DeleteCheck 方法中将事物的 ID 和模型本身传递给控制器。
@model TestManagementTools.Models.Checklist
@if (Model != null) {
for (int i = 0; i < Model.Checks.Count; i++)
{
<tr>
<td>
@(i + 1)
</td>
<td>
@Html.TextAreaFor(model => model.Checks[i].Value, new { @class = "form-control" })
</td>
<td>
//пробуем передать
<a href="@Url.Action("DeleteCheck", "Checklists", new { checklist = Model, id = Model.Checks[i].Id })" role="button">Delete</a>
</td>
</tr>
}
}
实际上,这里是ChecklistsController.cshtml控制器中的方法。
//добавление проверки по кнопке
[HttpPost]
public ActionResult GenerateChecks(Checklist checklist)
{
if (Request.IsAjaxRequest())
{
Check check = new Check();
if (checklist.Checks == null)
{
checklist.Checks = new List<Check>();
}
checklist.Checks.Add(check);
return PartialView("_checklist", checklist);
}
return PartialView("_checklist", checklist);
}
//удаление проверки, но сюда приходит модель равная null
public ActionResult DeleteCheck(int id, Checklist checklist)
{
checklist.Checks.RemoveAt(id);
return PartialView("_checklist", checklist);
}
所以问题是如何将事物和模型 ID 从我的局部视图传递到控制器?现在,如上所述,变为空
1 个回答