简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM