c# - 使用局部视图将模型错误消息添加到唯一索引
问题描述
我有由部分视图创建的模型列表,因此每一行都有一个唯一 ID 的索引。
我希望在此表单的帖子中将模型错误添加到列表中的字段。但是唯一的 id 没有进入控制器,所以我不能指定验证消息的键到字段
任何人都可以帮忙吗?
更新代码:
用表格查看
using (Html.BeginForm("Socio", "Student", FormMethod.Post, new { enctype = "multipart/form-data" ,@class= "SpForm" }))
{
@Html.AntiForgeryToken()
<div id="Studfindiv">
@foreach (var StudFin in Model.ListStudentFinances)
{
@Html.Partial("StudFinView", StudFin)
}
</div>
}
具有以下验证消息的部分视图:
@using HtmlHelpers.BeginCollectionItem
@model DataEntities.StudentFinance
<div class="studfin">
@using (Html.BeginCollectionItem("ListStudentFinances"))
{
<div class="text-center">
<h3><span class="box-title"></span> מס' @(Model.FinNo + 1)</h3>
</div>
<div class="text-center my-2">
@Html.DropDownListFor(model => model.Year, ViewBag.YearsList as SelectList, "", htmlAttributes: new { @class = "must control-label chosen", @data_placeholder = "שנה" })
@Html.ValidationMessageFor(model => model.Year, "", new { @class = "text-danger" })
@Html.ValidationMessageFor(model => model.Year, "", new { @class = "text-danger" })
</div>
<div class="text-center my-2">
@Html.DropDownListFor(model => model.Month, ViewBag.MonthsList as SelectList, "", htmlAttributes: new { @class = "must control-label chosen", @data_placeholder = "חודש" })
@Html.ValidationMessageFor(model => model.Month, "", new { @class = "text-danger" })
@Html.ValidationMessageFor(model => model.Month, "", new { @class = "text-danger" })
</div>
<div class="form-row text-center w-75 mx-auto">
@Html.EditorFor(model => model.Salary, new { htmlAttributes = new { @class = "must control-label pos-num" } })
@Html.LabelFor(model => model.Salary, htmlAttributes: new { @class = "title" })
@Html.ValidationMessageFor(model => model.Salary, "", new { @class = "text-danger" })
</div>
<p>קובץ <span class="box-title"></span></p>
<div class="half-row">
<div class="form-row chose-file-warp">
<div class="chose-file text-center">
@Html.TextBoxFor(model => model.FileSalary, new { @class = "must control-label", @type = "file", @accept = "image/jpeg,image/jpg,image/png,application/pdf", @style = "display:none;", @path = Model.PathSalary })
<label for="@Html.IdFor(m => m.FileSalary)">
<i class="ml-1 material-icons">add_photo_alternate</i>
בחר קובץ
</label>
@Html.ValidationMessage("Salary1", "", new { @class = "text-danger" })
</div>
</div>
<div class="text-center form-row ">
<a name="@Model.PathSalary" style="display:@(Model.PathSalary != null ? "" : "none")" class="btn btn-light btn-file">צפייה בקובץ שמור</a>
</div>
</div>
@Html.HiddenFor(model => model.FinNo)
}
</div>
以及在发布操作上添加验证消息的控制器:
[HttpPost]
[ValidateAntiForgeryToken]
[Authorize(Roles = "Student")]
public ActionResult Socio(SocioAdd socio, string uploadmethod)
{
foreach (var fin in socio.ListStudentFinances)
{
//until last year
if (fin.Year < DateTime.Now.Year - 1 || fin.Year == 0)
{
ModelState.AddModelError("Year", "שנה לא תקינה");
ok = false;
}
if (fin.Month > 12 && fin.Month < 1)
{
ModelState.AddModelError("Month", "חודש לא תקין");
ok = false;
}
if (fin.Salary < 0)
{
ModelState.AddModelError("Salary", "הכנסה לא תקינה");
ok = false;
}
if (fin.FileSalary == null && fin.PathSalary == null)
{
ModelState.AddModelError("FileSalary", "חובה לצרף קובץ");
ok = false;
}
}
该列表具有由客户端中的部分视图创建的索引,并且要添加模型错误,我需要指定此 ID,如图所示。
任何人都可以帮我添加验证消息吗?
解决方案
推荐阅读
- javascript - AJAX (XMLHttpRequest) 进度监控不适用于 Service Worker
- python - 从列表中随机抽样数字,其总和至少需要大于给定的基准
- ibm-cloud - 使用 curl 获取 IAM 令牌失败
- mysql - 如何使用节点流将大数据插入mysql?
- java - java中数组列表的问题(总是将最后一个元素放入数组中)
- tensorflow - LSTM 模型加载产生奇怪的结果,不可序列化的关键字参数错误
- gatsby - GatsbyJS 优化何时获取图像
- c - Atmel AVR ADMUX 寄存器未分配值
- python - 如何在 Python 路径之间切换?
- javascript - 如何安装 jSweet?