3

The pattern I see in the majority of .NET MVC examples is to have your model be the object passed between the controller and lower layers and to form-bind directly to those. I opted for a domain-driven approach where I have domain objects passed between the layers. The idea was that these objects would be passed into the views.

The problem I'm running into is when it comes to pages with forms. I was intending to have separate objects for the forms to bind to on the way back (which would also contain the validation annotations). I was keeping that separate from the domain object because a single object could possibly be updated by different pages, with each page having it's own validation requirements (for example a person's address might not even be shown on one page, but required on another, so the validation wouldn't always work on the universal domain object). This complicates things once you postback the form and need to display errors.

Take for example an update page. I'd type the view to my domain Person object and the page would have it's field values populated from that. The postback action would take the form object for that page and validate that. If it passed I use automapper to copy the values to the domain object off the form and save. This all works. Where it breaks down is re-displaying that page when there are errors. If it's typed to the domain object I'd end up just repopulating the fields based on the old values instead of the values the user entered. If it's typed to the form object then I need to translate all my domain objects to these form objects for every page (and still possibly have to pass in the domain object if I need some values I'd be using read-only for that page).

I'm sure I'm overlooking / over-complicating something here.

Update An interesting find after playing around because of what @Charlino said. If I used the strongly typed html helpers to make the inputs (Html.TextBoxFor()) it will remember the values even if the view is typed to the domain object. If I use the generic ones (Html.TextBox()) or raw HTML it doesn't seem to.

6
  • Is your "form object" also a Person object? If so, just return that to the view. Commented Dec 1, 2010 at 16:18
  • @paul the form object is a subset of the person object -- only the fields I expect to get over HTTP Post and want to validate. Commented Dec 1, 2010 at 16:22
  • Have you actually implemented this? Because from my understanding when you re-display the page it should check ViewState to repopulate the fields and it shouldn't use the domain objects. On a side note, I agree with @qstarin's approach but I also agree with @jfar's comment. Commented Dec 1, 2010 at 17:35
  • @Charlino yes, I have it implemented. Maybe it isn't repopulating because I'm not using the HtmlHelpers for making inputs (I had switched to raw HTML because I wanted support for HTML5 attributes like placeholder). Commented Dec 1, 2010 at 18:15
  • @Charlino & @Parrots: As far as I know, ViewState is not supported in MVC. Commented Dec 1, 2010 at 18:47

1 Answer 1

3

Having a ViewModel object for each and every view is what I end up doing.

It is a bit more up-front work, as I have to define the ViewModel and mapping, but that code is so simple and straightforward it takes only seconds to write.

Often, when I need a form, I make a separate Form object, and have the ViewModel contain it.

public class MyViewModel 
{
    public string SomeNonFormDisplayValue { get; set; }
    public bool AnotherDisplayOnlyValue { get; set; }
    public IEnumerable<Tuple<int, string>> SelectionListItems { get; set; }

    public MyViewsForm Form { get; set; }
}


public class MyViewsForm
{
    public string EditableProperty { get; set; }
    public int SelectionListItemId { get; set; }
}

I type my View to the ViewModel, but I make the Post Action Method take just the form.

public class MyController
{
    [HttpGet]
    public ActionResult Edit() { ... }

    [HttpPost]
    public ActionResult Edit(MyViewsForm form) { ... }
}

I also make query methods to retrieve the ViewModel, one that populates the form and another that does not - for when I return the POST'd form with errors.

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

8 Comments

Voting up for the technique but with the caution that a form object inside the ViewModel is unnecessary and overly complicated.
I've tried both ways and prefer the nested form object. What is complicated about it? It is only one additional class with nothing more than property getters & setters. And I find it helps eliminate any confusion involved with posting an object that is only half needed.
I too agree with this technique and I also kinda agree with @jfar's comment. I use to have my form models inside my view models but changed last night and it feels like some shackles have been broken.
@Charlino: would you care to elaborate about how exactly you feel shackles are broken by not using a nested Form object? This is something I've moved to more recently, so I am definitely open to re-evaluating.
@qstarin: In essence I guess it's the decoupling the form from the view and vice-versa which gives the most power & freedom. Now I have a Partial view which I expose through a Action method, so this action can be called/rendered from any view AND more importantly, it can be returned via ajax to the client to render as a modal form without code duplication.
|

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.