Using Unobtrusive JQuery validation in MVC3 without Ajax.BeginForm?
I just ported my application to mvc3 and i have a lot of view using jquery form plugins. I would like to use the unobtrusive jquery validation provided by MVC3 but i dont want to use Ajax.BeginForm or Html.BeginForm. Is there a way of having the html5 tag rende开发者_开发知识库red alone ?
Well, from a quick search in the source... You can't without hacking around a bit. At least not from the server side.
From what I understand the unobtrusive validation uses the Data-* attributes rendered for each input when using the helpers, and only when a FormContext exists in the ViewContext.
You could create a FormContext in the ViewContext yourself (if you're using EditorFor), it might work. This.ViewContext.FormContext = new FormContex() might work.
As an alternative you can call @Html.GetUnobtrusiveValidationAttributes() and do it yourself, but I think that this with this kind of refactoring using @Html.BeginForm() and Html.EditorFor() is easier.
Basically, it's more trouble than its worth, at least server side. Especially if you've already got validation running.
Maybe looking in the actual source of the unobtrusive JS is a better option, but that's out of my scope. Maybe someone else can help some more...
精彩评论