asp.net-mvcviewmodelviewbagstrongly-typed-view

Dynamic Referring URL in ViewBag or ViewModel?


I am working on a wizard like section of an ASP.Net MVC website that allows the user to post data and redirect to the next page in either a forward or backwards direction. That is if the user clicks next it saves and goes to the next page. If they hit back it saves and goes to the previous page. I am using a hidden field in the form to pass the value of the referring page. My question concerns the appropriate place to store the value of the referring page. I am using strongly typed views. Should I add a referring page field to my view model class for this page or is this a case where using ViewData is acceptable?


Solution

  • Its been more than 2 weeks since I asked this and no answers, but in the meantime my knowledge of ASP.Net MVC has evolved so I am going to answer my own question. First, I will mention that before I asked this question I had always used the ViewModel only and not ViewBag, so my instinct was to use ViewModel, but it seemed to me that that the information I needed to pass, the referring URL, was not something that belonged in the Model because it was not part of my data, it was just a piece of "helper" information that aided navigation. I think my hesitation to put the Referring Url in the ViewModel sprang from the fact that at the time my View Models were container classes that wholly incorporated domain model classes and since concerns of the view should not pollute the domain model, I did not want to put Referring Url in my domain model.

    Since then I have dropped the "View Models as containers for domain models" approach, and instead use custom View Models that map to domain models, but can also contain information not present in my domain model such as referring URL. I had already come to this conclusion when I found this post which asked a question similar to mine:

    ViewModels or ViewBag?

    This post reinforced to me that I was now on the right track and succinctly summarizes my current thoughts on the role of View Models: "That's what a view model is. A class that you specifically define to meet the requirements of your view."