Posts

Showing posts from April, 2013

How to Enable jQuery Client-Side Validation in Dynamically Rendered Partial Views in ASP.NET MVC

Recently I was doing some work in an ASP.NET MVC 4 website and was rendering a partial view to a div element via a jQuery AJAX call. I found that when the partial was included in a view from a controller's action method, all the client-side validation worked correctly, but if I rendered the partial view client-side code that was, for example, just getting the partial view by itself and putting into a div element as I was trying to do, the client-side validation wouldn't work.

After doing a little research, I learned that the client-side validation needed to be reset after the partial was rendered via the client-script. To that end, I wrote the following JavaScript function to allow quick and easy resetting of client-side validation in cases such as these. Just pass the CSS selector to the partial's parent form to the function shown below. This, of course, assumes that you also have references to the jQuery unobtrusive validation library.

function ResetValidation(formSelecto…

An ASP.NET MVC ActionButton HtmlHelper Extension Method

Here's a quick ActionButton HtmlHelper extension method I wrote. Hopefully this will come in handy to someone.

For more information on extension methods, check out Microsoft's MSDN document here, or check out one of my earlier posts here.
publicstaticMvcHtmlString ActionButton(     thisHtmlHelper htmlHelper, string value, object htmlAttributes, string actionName, 
string controllerName, object routeValues) {     if (String.IsNullOrWhiteSpace(actionName))         thrownewArgumentException("actionName is required.");     if (String.IsNullOrWhiteSpace(controllerName))         thrownewArgumentException("controllerName is required.");     var button = newTagBuilder("input");     button.MergeAttribute("type", "button");     button.MergeAttribute("value", value);     button.MergeAttributes(HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));     if (!String.IsNullOrWhiteSpace(actionName))     {         var urlHe…