I just realized that when I place a form tag on my layout page, surrounding the RenderBody section, the unobtrusive validation is not being generated. Something like this:
@using (Html.BeginForm())
{
<input type="submit" value="save" />
<div>
@RenderBody()
</div>
}
As you might have guessed I want to generate buttons over my content. Is this the correct unobtrusive's behavior?
BTW, If I place the form inside a particular page, everything works like a charm: the data-val* attributes are well generated.
I'll appreciate your valuable help.
best regards
Rodrigo
I've just run into the same problem, but possibly a better solution based on Darin Dimitrov's answer.
The trick is to create a page base type, based on the
WebViewPage<T>
class, the default base class for views and do theFormContext
swap there.And then in the
Web.config
file under the~/Views/
folder, alter thepageBaseType
attribute underpages
element, which can be found in thesystem.web.webPages.razor
section:While putting
@using (Html.BeginForm())
into the content page fixes the validation problem it also puts an extra set of<form>
tags into the output. I created a small extension that fixes the problem without writing anything to the output.Use it as
@using (Html.BeginSubForm())
Just add below code at top of the child view file...
its working fine for me.
i hope this will help you....
Thanks for your help, I tried it but I found a solution not as "grotesque" (as you said) as you suggested :D
I simply put a BeginForm method inside my page and also a BeginForm method on the layout:
so, at the end I have two BeginForm methods: ASP.NET MVC engine is using the one located on the layout page, so the data-val* attributes are being rendered correctly and the form is placed just where I wanted so any submit button on the layout can submit my particular page with the validations rendered
It works pretty well
Thanks a lot
regards, Rodrigo
You could apply a grotesque hack inside your view: