Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.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 ModelState.AddModelError-未显示任何错误_Asp.net Mvc_Asp.net Mvc 3_Modelstate - Fatal编程技术网

Asp.net mvc ModelState.AddModelError-未显示任何错误

Asp.net mvc ModelState.AddModelError-未显示任何错误,asp.net-mvc,asp.net-mvc-3,modelstate,Asp.net Mvc,Asp.net Mvc 3,Modelstate,在我的控制器中捕获异常后,我想为电子邮件字段设置错误消息: ... catch (EmailAlreadyExistsException emailAlreadyExistsException) { ModelState.AddModelError("Useraccount_Email", "This is an error message"); RegisterViewModel viewModel = new RegisterViewModel {

在我的控制器中捕获异常后,我想为电子邮件字段设置错误消息:

...
catch (EmailAlreadyExistsException emailAlreadyExistsException)
{
    ModelState.AddModelError("Useraccount_Email", "This is an error message");

    RegisterViewModel viewModel = new RegisterViewModel
    {
        Useraccount = useraccount,
        InformationMessages = InformationMessageHelper.GetInformationMessages()
    };
    return PartialView(viewModel);
}
...
错误消息现在应显示在我的视图中:

@using (Ajax.BeginForm("Register", new AjaxOptions { HttpMethod = "Post", OnSuccess = "registerAjaxSuccess" }))
{
    @Html.ValidationSummary(true)        
    <div class="ym-grid">
        <div class="ym-g33 ym-gl">
            <div class="ym-gbox">
                @Html.LabelFor(model => model.Useraccount.LastName)
            </div>
...
    <div class="ym-grid">
        <div class="ym-g33 ym-gl">
            <div class="ym-gbox">
                @Html.LabelFor(model => model.Useraccount.Email)
            </div>
        </div>
        <div class="ym-g33 ym-gl">
            <div class="ym-gbox">                  
                @Html.EditorFor(model => model.Useraccount.Email, new { required = "required" })                 
            </div>
        </div>
        <div class="ym-g33 ym-gr">
            <div class="ym-gbox">
               @Html.ValidationMessageFor(model => model.Useraccount.Email)
            </div>
        </div>
    </div>
...
@使用(Ajax.BeginForm(“Register”,新的AjaxOptions{HttpMethod=“Post”,OnSuccess=“registerAjaxSuccess”}))
{
@Html.ValidationSummary(true)
@LabelFor(model=>model.Useraccount.LastName)
...
@LabelFor(model=>model.Useraccount.Email)
@EditorFor(model=>model.Useraccount.Email,新的{required=“required”})
@Html.ValidationMessageFor(model=>model.Useraccount.Email)
...
但本例中未显示任何内容:(
我做错了什么?

在添加模型错误时,处理复杂对象时正确的分隔符是点
,而不是反分隔符

因此,以下呼吁应该有效:

ModelState.AddModelError("Useraccount.Email", "This is an error message");

请使用ModelState.AddModelError(string.Empty,“这是一条错误消息”); 和@Html.ValidationSummary(true)


这将起作用。

在您的视图中,
Html.ValidationSummary(true)
在哪里?在那里,请参阅更新的源代码。谢谢您,先生。这就成功了。是否有可能避免密钥的硬编码字符串,并从模型本身访问信息?