Solved

MVC3 Newbie Question about dialog messages

Posted on 2012-04-12
4
540 Views
Last Modified: 2012-06-21
Hi,
I am new to MVC (not web form). So when I first create MVC3 Application and choose Internet Template, VS generates a bunch of folders and files for me.

I noticed that the validation messages are scattered everywhere across the whole project in every logic layer. e.g.
in M - AccountModel you get:
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]

in V - Register.cshtml you get:
@Html.ValidationSummary(true, "Account creation was unsuccessful. Please correct the errors and try again.")
 
in C -  AccountController.cs you get:
switch (createStatus) {
                case MembershipCreateStatus.DuplicateUserName:
                    return "User name already exists. Please enter a different user name.";
                    .....


It is generated by VS, so I suppose it is a recommanded standard? I am scratching my head and asking myself, no way the industry will follow this spaghetti hard-coded messages in all layers? What is the .resx equivalent, and time and currency localization structure in MVC? Can I keep all these in the view layer, say, all in the .cshtml files, and not use controllers, or model properties/attributes to keep hard-coded messages?

I am a newbie, please help me :P
0
Comment
Question by:ThomasQ
  • 2
4 Comments
 
LVL 21

Expert Comment

by:Dale Burrell
ID: 37836417
MVC follows the DRY method (Don't Repeat Yourself).

Property validation is applied to properties in your model using attributes as you have indicated.

In the view you are telling the view where to display a summary of validation messages - same as in web forms. And in general you may tell the view where to display individual validations messages. Which is the correct logical way to do it, the model specifies what needs to be validated and how, the view specifies where and how to display validation messages.

In the controller you are carrying out checks/validation that are not specific to a property of the model, and in this case cannot be detected until you come to try and create the user. You can add an AJAX validation check to see if the user already exists, but at the end of the day you have to try a create the user with a given username before you can succeed of fail - you can't try that any earlier.

All very straight forward and certainly not spaghetti code :)
0
 
LVL 21

Accepted Solution

by:
Dale Burrell earned 500 total points
ID: 37836425
0
 
LVL 14

Expert Comment

by:binaryevo
ID: 37837633
dale_burrell hit the nail on the head the only thing i would have to add and is probably a personal preference more than anything, is the use of another design / sub pattern between your view and your controller called Model View View Model ( MVVM ).  Take a look at the links below they may help you:


http://stephenwalther.com/blog/archive/2009/04/13/asp-net-mvc-tip-50-ndash-create-view-models.aspx

http://blogs.microsoft.co.il/blogs/helpercoil/archive/2010/08/28/asp-net-mvc-and-the-mvvm-pattern.aspx
0
 

Author Closing Comment

by:ThomasQ
ID: 37840441
I just have not fit in the MVC design concept. Normally I would make the data layer and logic layer report enumeration values, then the UI would interrupt them into text messages.
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question