views:

29

answers:

1

Example is fake.

ASP.NET MVC: I have a view that renders a student info. Student info consists of First Name, Last Name and (optional) Age (declared as int?).

Student info is rendered something like, if age is not specified I want to show "not specified":

<span><%= Model.Student.FirstName %></span>
<span><%= Model.Student.LastName %></span>
<span><%= Model.Student.Age.HasValue ? Model.Student.Age.ToString() : "not specified" %></span>

My problem with text above is that it makes view too complicated (too complicated for my web designer to be able to modify it).

Question: Where could I move that decision?

  1. Property Student of model is a business object, and shall not contain details of rendering.
  2. Should I add a method to Model: Model.GetAgeAsText(Student student)? But then I end up with a host of loosely hanging methods on Model, that are crying to be grouped into something...but what?
  3. I could create a custom control StudentAgeShower, this appears to be the most OO-correct solution, but seems to be bit overkill.

Any suggestions?

+2  A: 

Create a View Model of a Student with all the decisions premade.

Keith Nicholas
I like, i like. And then expose `StudentViewModel` as a property of the `Model`? At what point is that `StudentViewModel` is created? and where does it live?
yes, but I would still call it "Student", the view model is created anywhere before its used.... usually if I'm decorating like that I go View(new ViewModel(realModel)) kind of thing (might be more involved) there are things like http://automapper.codeplex.com/ and the like to help some of these things, depends how often you need to do this, and how complex your objects are
Keith Nicholas
You handle date formatting same way?
No, generally not, I'd generally use a formatting function in the view for dates and times.
Keith Nicholas