?
Solved

MVC3 Newbie Question about dialog messages

Posted on 2012-04-12
4
Medium Priority
?
558 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 2000 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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

764 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