tags:

views:

186

answers:

2

Is there a easy way of checking a view model(Not Domain Model) for modifications in the post back?

    public ActionResult Billing()
    {
        var viewModel = new BillingViewModel();

        viewModel.prop1 = DomainService.Prop1 // Map Domain model to View Model

        return View(viewModel);
    }

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Billing(BillingViewModel viewModel)
    {
        //TODO: Check if ViewModel has changes and save to Domain Repository if valid
        if (ValidateBillingViewModel(viewModel))
        {

My homebrew solution would be to store a hash of model in hidden field and check it again, but is there a better option?

+2  A: 

The canonical way in MVC is to get (or store) the model server-side and use UpdateModel or model binding to update the fields on the newly retrieved (stored) model. Your ORM would be tasked with the responsibility of detecting if any of the properties on the model have changed so that it would know if any of the properties have changed. LINQ to SQL does this by invoking PropertyChanged handlers in the autogenerated model entity classes when properties are set to something other than the original value.

tvanfosson
My problem is that its costly to look up data again, just to see if user has modified any fields. Yes my ORM LINQ can take care of it, but it will hit DB for each request with a expensive aggregated sql which im not that keen on. Also storing it in Server cache would complicate matters, but it may be a viable solution if no other option presents itself.
Claus Thomsen
@Claus I believe you can have LinqToSql cache the results from your original query.
Todd Smith
@Todd The idea, was to avoid the round trip to DB. Linq2Sql's Datacontext is only open during 1 request and cache is closed when new request comes in for the update. I can implement a application query cache, but it adds complexity.
Claus Thomsen
+3  A: 

At this point, asp.net MVC has a rather anemic model story (i.e. it has none). The good news is that you can plug any ORM/DAL in that you like. The bad news is that the one supported by MS (Linq-to-SQL) has no dirty flag.

I would recommend checking out SubSonic, which is a fairly mature ActiveRecord implementation (which also has a dirty flag)

Matt Briggs