在ASP.NET Core中,is Bind[(exclude ..)]
并没有被移除。is Bind[(exclude ..)]
是用于在模型绑定过程中排除特定属性的特性。
以下是一个示例,演示如何使用 is Bind[(exclude ..)]
:
首先,创建一个用于模型绑定的模型类:
public class MyModel
{
public string Property1 { get; set; }
[BindNever]
public string Property2 { get; set; }
public string Property3 { get; set; }
}
在上面的示例中,Property2
被标记为 [BindNever]
特性,表示在模型绑定过程中不绑定该属性。
然后,在控制器中使用该模型类:
public class HomeController : Controller
{
public IActionResult Index()
{
return View();
}
[HttpPost]
public IActionResult Index([Bind("Property1,Property3")] MyModel model)
{
// 对绑定后的模型进行处理
// 注意:Property2 不会被绑定,因为它被标记为 [BindNever]
return View(model);
}
}
在上面的示例中,[Bind("Property1,Property3")]
表示只绑定 Property1
和 Property3
属性,而 Property2
将被忽略。
注意:is Bind[(exclude ..)]
特性是用于在模型绑定过程中排除属性的,而不是移除。