dan*_*e.f 10 c# asp.net-mvc data-annotations
我正在使用Data Annotations在ASP.NET MVC中验证我的模型.这适用于具有复杂参数的动作方法,例如,
public class Params
{
[Required] string Param1 {get; set;}
[StringLength(50)] string Param2 {get; set;}
}
ActionResult MyAction(Params params)
{
If(ModeState.IsValid)
{
// Do Something
}
}
Run Code Online (Sandbox Code Playgroud)
如果我想将单个字符串传递给Action Method(如下所示),该怎么办?有没有办法使用数据注释或我必须将字符串包装成一个类?
ActionResult MyAction(string param1, string param2)
{
If(ModeState.IsValid)
{
// Do Something
}
}
Run Code Online (Sandbox Code Playgroud)
我不相信您所提议的方法有数据注释方法。但是,如果您希望在调用操作方法之前进行验证,请考虑向参数添加自定义模型绑定程序属性并指定要使用的特定模型绑定程序。
例子:
public ActionResult MyAction [ModelBinder(typeof(StringBinder)] string param1, [ModelBinder(typeof(StringBinder2)] string param2)
{
.........
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5319 次 |
| 最近记录: |