如何在我的MVC4网页上显示ValidationSummary?

时间:2022-04-20 21:16:36

When I code my Razor HTML like this:

当我像这样编码我的Razor HTML时:

@using (Html.BeginForm(new { ReturnUrl = ViewBag.ReturnUrl })) {
   @Html.ValidationSummary()
}

I get the following which is just what I want:

我得到以下这就是我想要的:

<div class="validation-summary-valid" data-valmsg-summary="true">
<ul>
<li style="display:none"></li>
</ul>
</div>

However what I need for the form is the following:

但是,我需要的表格如下:

<form class="form" data-href="/User/Account/Register" id="registerForm">
   @Html.ValidationSummary()
</form>

With the above code I don't get the validation-summary-valid class. Can someone explain why this is or maybe suggest how I could code HTML.BeginForm so that I can get the form definition that I need.

使用上面的代码我没有得到validation-summary-valid类。有人可以解释为什么这是或者可能建议我如何编码HTML.BeginForm,以便我可以获得我需要的表单定义。

2 个解决方案

#1


0  

Use the overloaded BeginForm to include HtmlAttributes:

使用重载的BeginForm包含HtmlAttributes:

@using (Html.BeginForm("action", "controller", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form", @id = "registerForm", data_href = "/User/Account/Register" }))
{
    ....
}

The view engine won't parse "data-href" so you have to use an underscore instead of a hyphen.

视图引擎不会解析“data-href”,因此您必须使用下划线而不是连字符。

#2


0  

At the top of your view try the following

在视图的顶部尝试以下操作

ViewContext.FormContext = new FormContext();

ViewContext.FormContext = new FormContext();

#1


0  

Use the overloaded BeginForm to include HtmlAttributes:

使用重载的BeginForm包含HtmlAttributes:

@using (Html.BeginForm("action", "controller", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form", @id = "registerForm", data_href = "/User/Account/Register" }))
{
    ....
}

The view engine won't parse "data-href" so you have to use an underscore instead of a hyphen.

视图引擎不会解析“data-href”,因此您必须使用下划线而不是连字符。

#2


0  

At the top of your view try the following

在视图的顶部尝试以下操作

ViewContext.FormContext = new FormContext();

ViewContext.FormContext = new FormContext();