Claus Thomsen
Claus Thomsen

Reputation: 2355

Easy way to check for view model changes in asp.mvc

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?

Upvotes: 2

Views: 3416

Answers (2)

Matt Briggs
Matt Briggs

Reputation: 42188

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)

Upvotes: 3

tvanfosson
tvanfosson

Reputation: 532465

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.

Upvotes: 2

Related Questions