我对任务有看法。每个任务都有一个@Ajax.Action 链接,用于将此任务添加到检查列表。 我的观点:
@foreach (var item in Model.Tasks) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.TaskText)
</td>
<td>
@Html.DisplayFor(modelItem => item.TillDate)
</td>
<td>
@Html.EnumDropDownListFor(modelItem => item.State, new { id=item.Id, @class="state"})
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id = item.Id })
@Ajax.ActionLink("Add To check list", "AddToCheckList", new { id = item.Id }, new AjaxOptions { UpdateTargetId = "CheckListPartial" });
</td>
</tr>
}
我的 Controller Action :
public PartialViewResult AddToCheckList(int id)
{
context.AddTaskToCheckList(id);
return PartialView("_LoadCheckList", context.CheckList);
}
和 CheckList 类:
public class CheckList
{
public string Name { get; set; }
public List<Task> Tasks { get; set; } = new List<Models.Task>();
}
现在添加工作,但我有一个问题:我可以多次将一项任务添加到检查列表。 如何验证检查列表是否包含任务并显示错误消息?
更新: 我用我的 Controller 做了这个。验证有效,但未显示消息。
public PartialViewResult AddToCheckList(int id)
{
if(context.CheckList.Tasks.Exists(t=>t.Id==id))
ModelState.AddModelError("CheckList", "Check list already contains this task.");
if (ModelState.IsValid)
{
context.AddTaskToCheckList(id);
return PartialView("_LoadCheckList", context.CheckList);
}
return PartialView();
}
同时将此字符串添加到我的 View 中:
@Html.ValidationMessageFor(model => model.CheckList)
UPD2: 我的局部 View :
@model TaskTracker.Models.CheckList
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
Текст задания
</th>
<th>
Дата выполнения
</th>
<th></th>
</tr>
@foreach (var task in Model.Tasks)
{
<tr>
<td>
@Html.DisplayFor(modelItem=>task.Value.TaskText)
</td>
<td>
@Html.DisplayFor(modelItem => task.Value.TillDate)
</td>
</tr>
}
</table>
最佳答案
您不能在主视图的 @Html.ValidationMessageFor()
中显示来自部分的 ModelState
错误 - 这是 Razor 代码,在发送之前在服务器上解析到 View ,因此将仅显示来自主视图模型的 ModelState
错误。
一个选项包括将 ValidationMessageFor()
移动到分部 View ,唯一的缺点是无法定位元素。
但是,当您已经知道客户端中的所有值以将新行附加到表中时,返回整个添加任务表是不必要的。
更改您的代码以使用 $.ajax
方法并在该方法中返回一个 JsonResult
指示成功或否则
HTML
<a href="#" class="add" data-id="@item.Id">Add To check list</a>
脚本
var url = '@Url.Action("AddToCheckList")';
var table = $('#CheckListPartial table);
$('.add').click(function() {
var id = $(this).data('id');
var cells = $(this).closest('tr').find('td');
var text = cells.eq(0).text();
var date = cells.eq(1).text();
$.post(url, { id: id }, function(result) {
if (result) {
// create and append a new row
var row = $('<tr></tr>');
row.append($(<td></td>).text(text));
row.append($(<td></td>).text(date));
table.append(row);
} else {
// display your error
}
}).fail(function (result) {
// display your error
});
})
Controller 方法是
public JsonResult AddToCheckList(int id)
{
if(context.CheckList.Tasks.Exists(t => t.Id == id))
{
return Json(null); indicate error - show a hidden element containing a message
}
context.AddTaskToCheckList(id);
return Json(true); // indicate success
}
如果成功,您还应该考虑删除链接,这样用户就不会不小心再次点击它。
第三种选择是生成一个包含所有任务的 list 框,并允许用户选择或取消选择项目,然后发布表单并在一次操作中保存所有任务。引用this answer有关该方法的示例。
关于c# - 列表验证中的 Asp.NET 唯一项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44774006/