Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.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
Asp.net mvc mvc validationMessage在IEnumerable中时不工作<;T>;_Asp.net Mvc_Asp.net Mvc Validation - Fatal编程技术网

Asp.net mvc mvc validationMessage在IEnumerable中时不工作<;T>;

Asp.net mvc mvc validationMessage在IEnumerable中时不工作<;T>;,asp.net-mvc,asp.net-mvc-validation,Asp.net Mvc,Asp.net Mvc Validation,我有一门课: public class Cust { [Required(ErrorMessage ="NameField Req")] public string Name { get; set; } } 我使用这个类: public class CustModel { public IEnumerable<Cust> CustList { get; set; } } 当我点击提交按钮时,如果(ModelState.IsValid){}正在工作,但为什么

我有一门课:

public class Cust
{
    [Required(ErrorMessage ="NameField Req")]
    public string Name { get; set; }
}
我使用这个类:

public class CustModel
{
    public IEnumerable<Cust> CustList { get; set; }
}

当我点击提交按钮
时,如果(ModelState.IsValid){}
正在工作,但为什么我的错误消息没有显示出来?

我设法重新生成了您的代码,假设您的观点是:

请注意for循环

@model MVC_SteckOverflow.Models.ViewModels.CustModel
@using MVC_SteckOverflow.Models.ViewModels

@{
    ViewBag.Title = "ValidateIenumerable";
}

<style>
    .field-validation-error {
        color:#F00;
    }
    .input-validation-error {
        border: 1px solid red;
        background-color: #f9e2e2b3;
    }
</style>

<h2>ValidateIenumerable</h2>

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>CustModel</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @for (int i = 0; i != Model.CustList.Count(); i++)
            {
                @Html.EditorFor(x => x.CustList.ToList()[i].Name)
                @Html.ValidationMessageFor(x => x.CustList.ToList()[i].Name)
            }


            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section scripts{
    @Scripts.Render("~/bundles/jqueryval")
}
@model MVC\u SteckOverflow.Models.ViewModels.CustModel
@使用MVC_SteckOverflow.Models.ViewModels
@{
ViewBag.Title=“validateEnumerable”;
}
.字段验证错误{
颜色:#F00;
}
.输入验证错误{
边框:1px纯红;
背景色:#F9E2B3;
}
验证可枚举
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
客户模型

@Html.ValidationSummary(true,“,new{@class=“text danger”}) @for(int i=0;i!=Model.CustList.Count();i++) { @Html.EditorFor(x=>x.CustList.ToList()[i].Name) @Html.ValidationMessageFor(x=>x.CustList.ToList()[i].Name) } } @ActionLink(“返回列表”、“索引”) @节脚本{ @Scripts.Render(“~/bundles/jqueryval”) }
根据控制器中的以下操作:

    public ActionResult ValidateIenumerable()
    {
        CustModel custModel = new CustModel {
            CustList = new List<Cust> {
                new Cust{ Name = "A"},
                new Cust{ Name = "B"},
                new Cust{ Name = "C"},
            },
        };
        return View(custModel);
    }

    [HttpPost]
    public ActionResult ValidateIenumerable(CustModel custModel)
    {

        return View(custModel);
    }
public ActionResult validateEnumerable()
{
CustModel CustModel=新的CustModel{
CustList=新列表{
新客户{Name=“A”},
新客户{Name=“B”},
新客户{Name=“C”},
},
};
返回视图(custModel);
}
[HttpPost]
public ActionResult ValidateEnumerable(CustModel CustModel)
{
返回视图(custModel);
}
上面的代码已成功测试、构建和运行。


希望这有帮助……:)

您是否包含验证所需的js?如果我想添加和删除动态,请解释更多。上面的代码已经根据cshtml中的for循环使用动态列表:
@for(int i=0;i!=Model.CustList.Count();i++)
,因此您可以根据需要在控制器中动态创建任何
CustModel
    public ActionResult ValidateIenumerable()
    {
        CustModel custModel = new CustModel {
            CustList = new List<Cust> {
                new Cust{ Name = "A"},
                new Cust{ Name = "B"},
                new Cust{ Name = "C"},
            },
        };
        return View(custModel);
    }

    [HttpPost]
    public ActionResult ValidateIenumerable(CustModel custModel)
    {

        return View(custModel);
    }