示例代码:
// GET: /Movies/Edit/5 public ActionResult Edit(int? id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } Movie movie = db.Movies.Find(id); if (movie == null) { return HttpNotFound(); } return View(movie); } // POST: /Movies/Edit/5 // To protect from overposting attacks, please enable the specific properties you want to bind to, for // more details see http://go.microsoft.com/fwlink/?LinkId=317598. [HttpPost] [ValidateAntiForgeryToken] public ActionResult Edit([Bind(Include="ID,Title,ReleaseDate,Genre,Price")] Movie movie) { if (ModelState.IsValid) { db.Entry(movie).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(movie); }
注意下面的代码:Bind(Include="ID,Title,ReleaseDate,Genre,Price")] Movie movie 第一次见,发现可以这样写。
链接:https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/implementing-basic-crud-functionality-with-the-entity-framework-in-asp-net-mvc-application#overpost
请注意第二个 Edit
操作方法的前面是 HttpPost
特性。 此属性指定的重载Edit
可以仅针对发出的 POST 请求调用方法。 您可以应用HttpGet
属性与第一个编辑方法,但是,它们是不必要,因为它是默认值。 (我们将引用的操作方法的隐式分配HttpGet
属性为HttpGet
方法。)绑定属性是黑客可以防止过度发布到您的模型数据的另一个重要的安全机制。 在你想要更改的绑定属性中,应仅包含属性。 你可以阅读过多发布和中的绑定属性我过多发布安全说明。 在本教程中使用简单模型中,我们将绑定模型中的所有数据。 ValidateAntiForgeryToken属性用于防止请求伪造,并使用成对出现@Html.AntiForgeryToken()
在编辑视图文件 (Views\Movies\Edit.cshtml),如下一部分所示:
@Html.AntiForgeryToken()
生成必须匹配隐藏的窗体防伪令牌Edit
方法的Movies
控制器。你可以阅读更多有关跨站点请求伪造 (也称为 XSRF 或 CSRF) 在我的教程MVC 中的 XSRF/CSRF 防护。
https://docs.microsoft.com/zh-cn/aspnet/mvc/overview/getting-started/introduction/examining-the-edit-methods-and-edit-view
这个教程有关于验证信息国际的部分,需要认真学习下。
同事教程内容非常丰富,需要多看看。
原文:https://www.cnblogs.com/Tpf386/p/10186791.html