This is a migrated thread and some comments may be shown as answers.

Validating Dynamically added controls in ASP.NET MVC

1 Answer 219 Views
TabStrip
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Kevin
Top achievements
Rank 1
Kevin asked on 29 Jul 2011, 08:12 PM

I can't get validation to work.  I am new to ASP.NET MVC.  I am not sure if this is related to the Telerik Tabstrip or not.  I have see similar posts about validation not working with multiple tabs or with loadFromContent tabs, etc...

http://www.telerik.com/community/forums/aspnet-mvc/tabstrip/form-validation-does-not-work-with-multiple-tabs.aspx
http://www.telerik.com/community/forums/aspnet-mvc/tabstrip/client-validation-not-working-on-loadfromcontent-tab.aspx
http://www.telerik.com/community/forums/aspnet-mvc/tabstrip/repost-client-validation-in-loadfromcontent.aspx

None of these solutions work for me.  I have one added complication.  Not only are my controls in a partial view within a tab, the tab is dynamically added. So all of my controls that I want to validate are dynamically created.  Server side validation works, but not client-side.

My model has data annotations like "[Required]", and I have controls like Html.ValidationSummary and Html.ValidationMessageFor (for each field in your model) and I've been told it's supposed to work like magic! But it doesn't :(

Any ideas why client-side validation is not working? Do I have to do something special because the controls were created dynamically?

Steve

1 Answer, 1 is accepted

Sort by
0
Marc Simkin
Top achievements
Rank 2
Iron
Iron
Veteran
answered on 07 Aug 2011, 05:07 PM
Steve:

All you need to do is hook up the validations after the content is done loading.

On the tab, hook up the Client Event OnContentLoad.  Then in a javascript file just  place the following:
function onContentLoad(e) {
    $.validator.unobtrusive.parse("form");
}

The validation framework should be all hooked up.

Marc
Tags
TabStrip
Asked by
Kevin
Top achievements
Rank 1
Answers by
Marc Simkin
Top achievements
Rank 2
Iron
Iron
Veteran
Share this question
or