views:

77

answers:

4

Hey guys

I'm really new to both ASP.NET MVC and the MVC pattern generally.

For context, I'm currently running .NET 4.0, MVC 2.

I know that MCV 2 has a bunch of built in features for:

  • validation (both client and server side, through a variety of ways)

  • error handling (via attributes and other methods)

But what should be used to return feedback to the user that is neither an error nor validation?

For example, I have a simple form that returns a csv (using myController.base.file()) IF data is found.

If data is not found, I'd like to return the View with the Model as-is plus a message, like, "no data found, try a different date range"

Now,

should that kind of feedback message be stored in the model itself?, or

is there a cleaner mechanism for this?

Is that what the ModelStateDictionary is for?

UPDATE

just to clarify. I know there may be many ways to do what I want, but what's the correct MVC way of doing it.

Keep in mind the feedback data is not an error, and I don't want to redirect to some other view.

+1  A: 

Validation errors directly stemming from user actions on your model (e.g. too short a password)
is at home as close to the model as possible.

General error messages of the "no data found" kind are easier addressed through having a dedicated Error View of some sort.


Added:

If I understand you correct, you prefer to stick with one view; be it validation gold or burning SQL servers ;-)

Not madly MVC2 experienced mysef, but being consistent and always create own models, never what you might be served by what(not) ORM you're using (Linq<->SQL, EF) should give you the fine-graininess you need.

If my memory serves me correct I think the Nerddinner could be enlightening; and if you really
want to take the plunge, why don't go Deep Inside ASP.NET MVC 2 Model Metadata and Validation


I've gotten my MVC mainly from books, but the blogosphere has definitely started producing golden material on the more "foreign" parts of ASP.NET MVC.
My real aha moment regarding the M of MVC came while reading
Steve Michelotti's ASP.NET MVC View Model Patterns (a great source for MVC2 info all round)

Morten Bergfall
thanks morten, that was my first thought. however, it's not really ab error, and I don't want to have to add an extra click, I don't think it's a nice user experience. with this in mind, whats the right thing to do? also could you address the whole question? cheers
andy
"If I understand you correct, you prefer to stick with one view; be it validation gold or burning SQL servers": nope, I'm talking about scenarios where it is neither an error nor a validation issue. The Model as I understand is "data" abstracted from the view, that the controller and view pass around to each other. My feedback data feels like it shouldn't be in the model...
andy
the other way around; the Model, or ViewModel, is (meant to be) to be your blueprint for how the data you pass to the View is structured.
Morten Bergfall
cool, thanks for the clarification Morten, great link too, cheers!
andy
my pleasure; MVC has been somewhat of a hurdle to learn to use productively, but the way ahead is golden ;-)
Morten Bergfall
+1  A: 

If you're talking about a message that you want to code for somewhere in your view, you ought to have that on your model and have your view consume it.

If you want to be able to handle system messages generally in the same way across your application (with a message at the top or side of the window, e.g.), you might create a utility method that puts the information in ViewData with a special key that could get picked up by your master page. If you use TempData instead, the message could be persisted across a redirect.

StriplingWarrior
thanks stripling warrior, as I've said above, I'm quite new to asp.net mvc 2, could you clarify what the ViewData is, what is it's place in the MVC pattern?
andy
The Model as I understand is "data" abstracted from the view, that the controller and view pass around to each other. My feedback data feels like it shouldn't be in the model...is that a wrong assumption/feeling?
andy
Larsenal explained it pretty well. Your ViewModel should be a data-based representation of what you are hoping to show in the View, without specifying *how* it will be displayed. The Controller should contain the logic necessary to construct a ViewModel. The View's only concern should be rendering that model into HTML. So if the View should be aware of the messages you're passing, the messages should be on the ViewModel.
StriplingWarrior
ViewData is a more "loosely coupled" way to pass data to the View layer. As such, it can be useful for passing information that isn't directly related to the ViewModel. If you want to pass messages to your master page, for example, to ensure that they are displayed consistently across your application, the ViewData dictionary is a good place to store them. Just make sure that your master page is smart enough not to fail when there are no messages present. And it helps to abstract the message addition/retrieval logic into a utility method so you're not using magic key strings everywhere.
StriplingWarrior
+2  A: 

I think what might clear the air is the idea of a ViewModel. Except for the most simple scenarios, you'll find more than one kind of model in your project. They can go by many names, but I find these names helpful:

Pure domain models (Models)
This is where you have your ideal representations of our data model.

Application models (ViewModels)
These models account for the realities of displaying your domain model in a view. They contain data specifically needed for a specific view. It's perfectly acceptable to put something like a status message in this kind of a model.

I would recommend this insightful blog post which shows when, why and how to use ViewModels.

Example:

public class WidgetDataExportViewModel {
  public DateTime StartDate {get;set;}
  public DateTime EndDate {get;set;}
  public MyEnum SomeOtherProperty {get;set;}
  public string StatusMessage {get;set;}
  public IEnumerable<Widget> Widgets {get;set;}

}
Larsenal
ah ha! finally! excellent, cheers. so, my form currently is a "data export" form, and is very simple. The view has two date fields, and a radio button group, and the Model reflects this by having two datetime properties and an enum property (which binds to the radio buttons). Now, that Model, is that a Pure Domain, Application, or View Model? could you expand? cheers!
andy
I changed the labels to clarify a bit, but I'll add more.
Larsenal
excellent link to the blog post, and great answer to my question, thanks! The main take away for me here is the clarification of the idea that the ViewModel should really meet all the data requirements of the View... this makes it all much clearer, thank you!
andy
+1  A: 

I have previously used ModelState.AddModelError successfuly to show a summary message. Just make sure you use a key that is not a Model field name.

Note: I have adapted my design from Steven Sanderson's book (see the index on RulesException)

Good luck

Syd
excellent advice, thanks dude! good stuff
andy