Solved

MVC3 Newbie Question about dialog messages

Posted on 2012-04-12
4
535 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Visual Studio 2013 and Eclipse Neon 6 37
jquery datatables, asp.net MVC 9 41
Alert on Server memory 2 22
Please explain the parts of these 2 LINQ expressions 3 30
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

773 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