如何在ASP NET MVC 6中更新模型? [英] How to Update Model in ASP NET MVC 6?

查看:88
本文介绍了如何在ASP NET MVC 6中更新模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

方案:如何更新模型?

Scenario: How to update a model?

ASP MVC 6

ASP MVC 6

我正在尝试更新模型.为了将模型信息传递给客户端(浏览器/应用程序),我正在使用DTO.

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

问题1:要进行更新,我应该将整个对象放回去吗?

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

问题2:是否有一种方法可以让我轻松地仅传递已更新的信息?如果是,怎么办?

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

问题3:我可以使用JSON补丁进行更新吗?

Question 3: Can I use JSON Patch for updation?

推荐答案

问题2:是否有一种方法可以让我轻松地仅传递那些 更新了吗?如果是,怎么办?

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;}
}

在您的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");
}

和视图

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

和您的HttpPost操作

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
}

假设yourUserRepository是数据访问类抽象的对象.

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

问题1:要进行更新,我应该将整个对象放回去吗?

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

取决于最终用户的需求.通过这种视图模型方法,它将仅发布Id和LastName,这就是我们的用例.

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.

我可以使用JSON补丁进行更新吗?

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.

如果需要,您可以简单地序列化表单数据(仅具有ID和LastName),然后使用jQuery post方法将其发送到服务器.

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.
    });

  });

});

为防止 overposting ,您可以在HttpPost操作方法上使用Bind属性使用绑定白名单.但是最安全的策略是使用与客户端允许发送的内容完全匹配的视图模型类.

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.

这篇关于如何在ASP NET MVC 6中更新模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆