我有部分视图创建的模型列表,因此每一行都获得唯一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。
任何人都可以帮助我向其中添加验证消息吗?