Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将模型错误消息添加到具有部分视图的唯一索引_C#_.net_Asp.net Mvc 5_Frameworks_Entity - Fatal编程技术网

C# 将模型错误消息添加到具有部分视图的唯一索引

C# 将模型错误消息添加到具有部分视图的唯一索引,c#,.net,asp.net-mvc-5,frameworks,entity,C#,.net,Asp.net Mvc 5,Frameworks,Entity,我有一个由部分视图创建的模型列表,所以每一行都有一个唯一id的索引 我想在此表单的帖子中向列表中的字段添加模型错误。但是控制器中没有唯一的id,因此,我无法将验证消息的键指定给字段 有人能帮忙吗 更新代码: 用表单查看 using (Html.BeginForm("Socio", "Student", FormMethod.Post, new { enctype = "multipart/form-data" ,@class= "SpForm" })) { @Html.AntiForge

我有一个由部分视图创建的模型列表,所以每一行都有一个唯一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>

}

使用(Html.BeginForm(“social”、“Student”、FormMethod.Post、new{enctype=“multipart/formdata”、@class=“SpForm”}))
{
@Html.AntiForgeryToken()
@foreach(Model.ListStudentFinances中的var StudFin)
{
@Html.Partial(“StudFinView”,StudFin)
}
}
包含以下验证消息的局部视图:

@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>
@使用HtmlHelpers.BeginCollectionItem
@DataEntities.StudentFinance模型
@使用(Html.BeginCollectionItem(“ListStudentFinancials”))
{
מס'@(型号:芬诺+1)
@Html.DropDownListFor(model=>model.Year,ViewBag.years作为SelectList列出,“”,htmlAttributes:new{@class=“必须控制选定的标签”,@data_placeholder=“שנה”})
@Html.ValidationMessageFor(model=>model.Year,“,new{@class=“text danger”})
@Html.ValidationMessageFor(model=>model.Year,“,new{@class=“text danger”})
@Html.DropDownListFor(model=>model.Month,ViewBag.monthList作为SelectList,“”,htmlAttributes:new{@class=“必须控制选定的标签”,@data_placeholder=“••דש”})
@Html.ValidationMessageFor(model=>model.Month,“,new{@class=“text danger”})
@Html.ValidationMessageFor(model=>model.Month,“,new{@class=“text danger”})
@EditorFor(model=>model.Salary,new{htmlAttributes=new{@class=“must control label pos num”})
@LabelFor(model=>model.Salary,htmlAttributes:new{@class=“title”})
@Html.ValidationMessageFor(model=>model.Salary,“,new{@class=“text danger”})
קובץ

@Html.TextBoxFor(model=>model.FileSalary,新的{@class=“必须控制标签”、@type=“file”、@accept=“image/jpeg、image/jpg、image/png、application/pdf”、@style=“display:none;”、@path=model.PathSalary}) 添加照片 בחר קובץ @ValidationMessage(“Salary1”,new{@class=“text danger”})


有人能帮我添加验证消息吗?

你能添加一些代码以便更好地解释吗?我们都会从中受益!我已经添加了验证消息,你能帮我吗?
[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;
                    }
}