0

When I code my Razor HTML like this:

@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.

  • Maybe http://stackoverflow.com/a/6813634/639343 can help you – archil Jan 01 '13 at 20:31
  • @archil - I looked at this but it still doesn't really explain why I don't see any validation div if I just specify form and don't use Html.BeginForm. –  Jan 01 '13 at 20:37
  • Maybe you should initialize FormContext. That is what Html.BeginForm does – archil Jan 01 '13 at 21:28

2 Answers2

0

Use the overloaded BeginForm to include 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.

viperguynaz
  • 12,044
  • 4
  • 30
  • 41
0

At the top of your view try the following

ViewContext.FormContext = new FormContext();

Adam Tuliper
  • 29,982
  • 4
  • 53
  • 71