3

Scenario: How to update a model?

ASP MVC 6

I am trying to update a model. For passing the model information to the client(browser/app) I am using the DTO.

Question 1: For updating, should I post the whole object back?

Question 2: Is there a way I can easily pass only the information that is updated? If yes, how?

Question 3: Can I use JSON Patch for updation?

Rahul Kushwaha
  • 421
  • 7
  • 14

2 Answers2

1

Question 2: Is there a way I can easily pass only the information that is updated? If yes, how?

Yes. You should create a view model which should have only those properties needed for the view.

Let's assume your use case is to build a view which allows user to edit only their last name.

public class EditUserViewModel
{
  public int Id {set;get;}
  public string LastName {set;get;}
}

And in your Get

public ActionResult Edit(int id)
{
  var user = yourUserRepository.GetUser(id);
  if(user!=null)
  {
   var v = new EditUserViewModel { Id=id,LastName=user.LastName};
   return View(v);
  }
  return View("NotFound");
}

And the view

@model EditUserViewModel
@using(Html.BeginForm())
{
  @Html.TextBoxFor(s=>S.LastName)
  @Html.HiddenFor(s=>s.Id)
  <input type="submit" id="saveBtn" />
}

and your HttpPost action

[HttpPost]
public ActionResult Edit(EditUserViewModel model)
{
   // Since you know you want to update the LastName only, 
   // read model.LastName and use that
   var existingUser = yourUserRepository.GetUser(model.Id);
   existingUser.LastName = model.LastName;
   yourUserRepository.Save();
   // TO DO:  redirect to success page
}

Assuming yourUserRepository is an object of your data access classes abstraction.

Question 1: For updating, should I post the whole object back?

Depends on what you want from the end user. With this view model approach, It is going to post only the Id and LastName and that is our use case.

Can I use JSON Patch for updating?

Since you are only sending the data which needs to be updated (partial data), you should be fine.

If you want,you may simply serialize your form data(which has only Id and LastName) and use jQuery post method to send it to your server.

$(function(){

  $("#saveBtn").click(function(e){

    e.preventDefault(); //prevent default form submit

    var _form=$(this).closest("form");
    $.post(_form.attr("action"),_form.serialize(),function(res){
      //do something with the response.
    });

  });

});

To prevent overposting, you can use a binding whitelist using Bind attribute on your HttpPost action method. But the safest strategy is to use a view model class that exactly matches what the client is allowed to send.

Shyju
  • 214,206
  • 104
  • 411
  • 497
  • You have clarified most of my doubts. But I was wondering if I should use JSON Patch to send the update information? – Rahul Kushwaha Dec 07 '15 at 03:50
  • 1
    Since you are sending only the needed data. Simply post your form that should be good enough – Shyju Dec 07 '15 at 03:51
  • Actually there is one more thing. I also have an app which will have the same operations. Should I use the same ViewModel approach for the app or use JsonPatch? Or if you can elaborate on some scenarios where JSON Patch can be used? – Rahul Kushwaha Dec 07 '15 at 03:55
  • Seems like this approach is fine if you have one property you want to update... what if you have 20? And what if they change fairly frequently? Seems like a maintenance nightmare when UpdateModel(...) just worked – Greg Ennis Mar 09 '16 at 02:54
0

Instead of this

UpdateModel(model);

You now can call this

await TryUpdateModelAsync(model);
Greg Ennis
  • 14,917
  • 2
  • 69
  • 74