Search code examples
asp.netvalidationasp.net-mvc-3partialunobtrusive-javascript

Force unobstructive syntax without Html.BeginForm / Ajax.BeginForm in partial view


When I put a part of my form in a partial view, all form parts get unobstructive syntax except the form elements in the partial view.

The only way I found how to "apply" the unobstructive syntax, is by starting another form inside the partial view.

View:

@using (Ajax.BeginForm("SubmitHandler", new DefaultAjaxOptions()))
{
    @Html.EditorFor(m => m.Name)
    @Html.Partial("MyPartialView", Model)
}

PartialView:

@Html.TextBoxFor(m => m.SomeContent)

Output:

<input class="text-box single-line" data-val="true" data-val-required="This field is required." id="Name" name="Name" type="text" value="">
<input id="SomeContent" name="SomeContent" type="text" value="0">

So only the input element from the View has the unobstructive syntax and the partial view hasn't...

Is there a way to apply unobstructive syntax inside a partial view, wich doesn't require you to begin a new form?


Solution

  • I actually found a better solution! Went digging a bit in the asp.net mvc code and the MvcForm class creates all the unobstructive validation syntax.

    so if you use:

    @using(new MvcForm(ViewContext))
    {
    }
    

    instead of

    @using(Html.BeginForm())
    {
    }
    

    it will still apply the unobstructive syntax, but won't create the form html tags :)