Showing posts with label MVC. Show all posts
Showing posts with label MVC. Show all posts

Extending EntLib VAB - Part 1: Enable client side support EntLib VAB and align the validation error keys with MVC ModelState keys

13 comments


Note: Thank you for visiting my blog. However, this blog is not maintained actively anymore. This post is now also in my new blog. Feel free to leave a comment there.

As discussed in my last post EntLib VAB doesn't support client side validation (in js/jQuery) out-of-the-box. And also while validating object graph, EntLib VAB generate Validation Result keys which are different from the ModelState keys in MVC.

In this post we will discuss on this problem.

Solution to this problem is simple. We just need to create an adapter for EntLib VAB validator to MVC ValidationAttribute, i.e. a piece of code is needed that will read the EntLib Validation configuration xml and for each validator it will return the MVC counterpart of the validator, i.e. ValidationAttribute.

As for aligning the validation error keys with MVC ModelState keys, we need to leverage the default model validation of ASP.NET MVC.

And the great news is that one single piece of code addresses combines these two solutions.

ASP.NET MVC: ActionNameSelector and ActionMethodSelector (or another approach to submit form to multiple action methods) – Part II

0 comments


Note: Thank you for visiting my blog. However, this blog is not maintained actively anymore. This post is now also in my new blog. Feel free to leave a comment there.

Hope you enjoyed the first part of this blog post.

In this part I’ll discuss about how you can make your own ActionMethodSelector Attribute (though I'll use the out-of-the-box ActionNameSelectorAttribute ActionNameAttribute).

Lets first discuss the problem statement: I’ve one form which has two radio buttons and I want the form to be posted to two different Action Methods based on selection.

ASP.NET MVC: ActionNameSelector and ActionMethodSelector (or another approach to submit form to multiple action methods) – Part I

0 comments

Note: Thank you for visiting my blog. However, this blog is not maintained actively anymore. This post is now also in my new blog. Feel free to leave a comment there.

At work I have recently faced a requirement to post a single form to multiple Action methods in a controller based on the context. For example, in the view there is a Form and there are several radio buttons; now my form should be posted to the Action method based on the radio button selection.
As usual I Googled it and found that internet is glutted with various approaches. Some of them are: 1. Posting to single action method and use a switch mechanism inside it, 2. Using multiple forms, 3. Using named submit button and assigning different values to those.
However there is one concept that I liked most and that is making use of ActionNameSelector and ActionMethodSelector.

What are these?
Well these are attributes that influence/affects the selection of action methods… yes you got it right. I’ve quoted it from MSDN and just like some of you (maybe) I didn't understood a thing when I read those lines... ;)
You can check the below MSDN links:
ActionNameSelectorAttribute Class, ActionMethodSelectorAttribute Class.
Hmmm… so what are these anyway. 

Customizing Validation Attributes in MVC

2 comments

Note: Thank you for visiting my blog. However, this blog is not maintained actively anymore. This post is now also in my new blog. Feel free to leave a comment there.

Background

As many of you already know, MVC provides validation attribute functionality to validate view models. A few very commonly used validation attributes are: Required, RegularExpression and Range. Below is one simple example:


public class Person
    {
        [Required(ErrorMessage = "Please Enter your name")]
        public string Name { get; set; }
 
        [Required(ErrorMessage = "Please Enter your Email")]
        [RegularExpression(".+\\@.+\\‥+", ErrorMessage = "Please Enter valid Email")]
        public string Email { get; set; }
 
        [Required(ErrorMessage = "Please Enter your Phone")]
        public string Phone { get; set; }
    }


Which will give you the following output:
This default behavior is good for less complex application, where you can apply factory made validation without any considerable effort from your side.

You can also enable client side validation(validation will be done at client side without server post back) by adding/modifying the following lines in the
1. Web.config file:
<configuration>
  <appSettings>
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>
</configuration>

2. Layout / View page (of course you can use the latest version of jQuery):
    <script src="~/Scripts/jquery-1.9.1.min.js"></script>
    <script src="~/Scripts/jquery.validate.min.js"></script>
    <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>


So far so good, Now lets complicate it

Lets assume that this application is going to be used by people from different regions and every region has a specific format for phone number. Every region has a separate set of special characters to be allowed for the phone number.