Solved

MVC3 Newbie Question about dialog messages

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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 …
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

708 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now