默认情况下,当我使用Entity Framework创建带有CRUD ActionResults的控制器时,它们看起来像这样:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "ID, Property1, Property2")] ClassName object)
{
// do whatever
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "ID, Property1, Property2")] ClassName object)
{
// do whatever
}
[Bind(Include = "ID, Property1, Property2")]
是否必要?为什么我不能这样做?
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(ClassName object)
{
// do whatever
}
答案 0 :(得分:0)
你可以在没有Bind的情况下完成它,但请注意它会将Edit函数暴露给潜在的参数注入。为清楚起见,让我们说你传入一个类,Foo和Foo有其他属性,比如Property3和Property4,然后通过不用Bind限制方法,该方法将允许和接受这些额外的值属性,可能不是您想要/想要的。
TL; DR:没有必要绑定。