MVC 6绑定属性消失?

请原谅我的noob问题,但我注意到绑定属性不再作为控制器模板默认出现在MVC 6中。

我知道我的属性仍然存在,但我们仍然需要使用它们吗? 我听说他们可以用来防止过度发布攻击。 他们是否删除它,因为MVC 6可以找出方法来防止这种情况,而不使用它们? 还是有更安全的方法来防止这种情况?

防止重新发布的最好方法是获取实体, 只更新需要更新和保存的属性

假设你有一个视图模型

public class CustomerViewModel { public int Id {set;get;} pubic String UserName {set;get;} public String FirstName {set;get;} public String LastName {set;get;} } 

并且假设有一个叫做Update的视图,它显示只读/只显示表单中的UserName和可编辑字段中的LastNameLastName 。 所以即使用户通过某种方式发布更新的用户名,我们也不应该更新该字段值。

 [HttpPost] public ActionResult Update(CustomerViewModel model) { var customer = yourDbContext.Customers.FirstOrDefault(s=>s.Id==model.Id); if(customer!=null) { // Updating only fields which are supposed to be updated from the view. customer.FirstName = model.FirstName; customer.LastName = model.LastName; yourDbContext.Entry(customer).State = EntityState.Modified; yourDbContext.SaveChanges(); return RedirectToAction("UpdatedSuccessfully"); } return View("NotFound"); }