Solved

MVC3 Newbie Question about dialog messages

Posted on 2012-04-12
4
552 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
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…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

724 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