Asp.net mvc 删除确认操作从中获取asp.net mvc中的Id
也许这个问题已经被问过了,但从未被回答过 deleteConfixed方法如何通过post方法获取idAsp.net mvc 删除确认操作从中获取asp.net mvc中的Id,asp.net-mvc,Asp.net Mvc,也许这个问题已经被问过了,但从未被回答过 deleteConfixed方法如何通过post方法获取id 没有包含Id的隐藏字段 我篡改了数据以更改referer Url,使其不再包含id,但deleteConfixed操作仍然在获取期间传递了正确的id 那它是从哪里来的 下面是代码,即get方法: [HttpGet] public ActionResult Delete(int? id) { if (id == null) {
- 没有包含Id的隐藏字段
- 我篡改了数据以更改referer Url,使其不再包含id,但deleteConfixed操作仍然在获取期间传递了正确的id
[HttpGet]
public ActionResult Delete(int? id)
{
if (id == null)
{
return RedirectToAction("Index");
}
Driver driver= db.Drivers.Find(id);
if (driver== null)
{
return RedirectToAction("Index");
}
return View(driver);
}
// POST: /Driver/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
Driver driver= db.drivers.Find(id);
db.drivers.Remove(driver);
db.SaveChanges();
return RedirectToAction("Index");
}
Html.BeginForm()
helper将根据您在RouteConfig.cs
假设您有默认的路由url:{controller}/{action}/{id},
那么如果您将一个值传递给(比如)5到Delete(int?id)
get方法,那么如果您检查表单标记,您将看到
。注意:如果传递到视图的模型具有名为id
的属性,也将添加此属性
发布表单时,
DefaultModelBinder
读取表单字段的值(Request.form
),还读取路由数据和查询字符串参数中的值,因此,即使您没有id
的隐藏输入,它也是从路由参数中设置的。什么“deleteConfixed”方法?分享一些代码,这样我们就可以知道你在引用什么。我检查了RoutedData dictionnary,它包含id,所以这是它的来源,但问题是,当我篡改数据和更改引用者时,怎么可能!感谢您的回答,我知道您所说的一切,除了检查表单标记,我认为id是以复杂的方式传递的,无法更改,在默认模板中id不是可选的,更改表单标记中的操作会导致服务器错误。。。再次感谢你的回答!