1

I am working on my model hierarchy and have a few questions.

1) I have a base model for each view type (Add, Edit and View). I put thing in these that are specific to each view type.

2) I then have a model base which the bases above inherit from. This allows me to include things that pertain to ALL views.

3) In my ModelBase, i have a couple of other view models like FeedbackViewModel, ShoppingCartViewModel, etc that I can consume on any view.

4) I have my MasterPage inheriting ModelBase.

Example

Public MustInherit Class ModelBase
    Public Property ErrorMessage As String
    Public Property InformationMessage As String
    Public Property WarningMessage As String
    Public Property FeedbackModel As New FeedbackViewModel

End Class

Public MustInherit Class ViewModelBase
    Inherits ModelBase

    'View Model Specific Stuff
End Class

'Allows contact us form to be submitted.
Public Class ContactUsViewModel
    Inherits ViewModelBase

    Public Property Name As String
    Public Property EmailAddress As String
    Public Property Phone As String
    Public Property Comments As String

End Class

That is the basic structure of my models, but a few questions:

1) What do I do with a view that requires no model, but I need to pass the FeedabckViewModel, SHoppingCartViewModel, etc.? I was thinking of a GenricViewModel?

2) Do you see any flaws in this design?

Thank You!

1 Answer 1

2

Some points:

  • Why use ErrorMessage, InformationalMessage, WarningMessage, etc. ModelState should be more than enough, and it ties in better with the validation helpers as opposed to you writing the manual stiching in the View.

  • I think a "base" model to handle different view types is a bit overkill. I think a enum specifying the mode would be better, then you can make decisions accordingly.

  • Overall, there is nothing really wrong with your design. It's a matter of opinion - for which mine is i usually create a ViewModel per View. Keeps it simple. I use areas extensively so it doesn't get messy. I generally try and create the ViewModels to make the View simple - that's the ultimate goal, not to re-use code across View's, but to keep the View simple. Such as augmenting the model with nested models to make use of partials/templates, as opposed to having a bunch of strings.

1) What do I do with a view that requires no model, but I need to pass the FeedabckViewModel, SHoppingCartViewModel, etc.?

Doesn't that kind of contradict itself? :) If you just need "parts" of a couple of ViewModels, either create another ViewModel, or if it's just a couple of fields, just stick it in the ViewData.

Sign up to request clarification or add additional context in comments.

5 Comments

Why use ErrorMessage, InformationMessage, etc? I do that to stash a message that I want to display on the view (page) that is not bound to a certain property, like Invalid Login.. In the controller I use the ModelState.AddModelError("Message","Invalid Login") then set a ValidationMessage for the Message property in the view.
So have a Enum ModelType and then in each model have a property ModelType? And are you saying nested models are good? I like them then you have a strongly typed model hierarchy.
Nested ViewModels are really good if you want to make use of convention-based templates, e.g custom display/editortemplates - Html.DisplayFor(model => model.NestedViewModelType).
Is there a way to inject a BaseModel that has properties that every view would need in say a BaseController?
yes, in a base controller you could override the OnActionExecuting method, and stick some data in the ViewBag.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.