Unobtrusive validation not working on jquery tabs

2019-09-18 09:18发布

问题:

I'm using jquery-ui tabs to display partial views. I have a view that contains the tabs definition:

<ul>
    <li><a href="@Url.Action("CreateTab","Catalogue")">Create</a></li>
    ...

And a controller method CreateTab which returns a PartialView:

public ActionResult CreateTab()
{
    var model = new StockCatalogueItemViewModel
    {
    ... populate some selectlists
    }
    return PartialView(model);

The model property that I'm validating has datatannotations on and I can see that the validation tags have been rendered in the HTML.

However the client-side validation simply isn't firing. The server-side validation is working fine and is correctly showing the invalid fields.

There is nothing wrong with the script bundles as validation works perfectly on other pages (albeit not using partial views), so I can only think it's something to do with the fact that I am using partial views. I've seen other posts related to this issue but none seem to help. Note that I'm NOT loading the partial views using ajax. I'm not doing anything complicated here, so why isn't it working?

回答1:

Since you're not using Ajax.BeginForm(), I'm pretty sure you need to use $.validator.unobtrusive.parse("#ID); in your javascrpt to specify that there's new validator information you want applied.