开发者

MVC3 Razor Ajax partial page validation

I have a wizard style page that I want to validate a page at a time. So before the user can move off the (partial) page I submit the inputs from th开发者_JAVA技巧e div for validation. Client side validation works OK but on some pages there are one or more fields I need to validate on the server. Yes I know about remote validation.

I can get the server side validation done without a problem. The issue I have is how do I display the errors on the correct fields? I can locate the fields in the div and find the span where the error message is suppose to go. But I just can't get the message to display.

I must be missing something when updating the field span. I would have thouht that there is a jQuery routine to add error information to a field. I need something similar to the controllers AddModuleError. So when I get return from my $.Post I can set error text on the appropriate fields.

Any suggestions?


A potential solution to your problem might be in this article "Client side validation after Ajax Partial View result in ASP.NET MVC 3"

Basically, once you get your html from the post you can invoke validation using jQuery.validator.unobtrusive.parse()

As per the example in the article;

   $.post("YourAction", { data: <your form data> },
  function(htmlContent){
    $('#container').html(htmlContent);
    jQuery.validator.unobtrusive.parse('#content')
  }

Worth looking into


If you are using the included RemoteAttribute, or the version I linked to in my answer to your other remote validation question, then you should not have to worry about displaying the error, as both work with the ValidationMessage helpers to automatically display errors.

Have you added Html.ValidationMessage(...) or Html.ValidationMessageFor(...) for each field to be validated?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜