.NET MVC ModelBinder基本使用

 Model Binder(模型绑定器),顾名思义,可以形象的理解为将数据绑定到一个 Model 的工具。这个 Model 是 Action 方法需要用到的某个类型(既可以是方法参数的类型也可以是方法内部对象的类型),要绑定到它上面的值可以来自于多种数据源。

   MVC 框架内置默认的 Model Binder 是 DefaultModelBinder 类。当 Action Invoker 没找到自定义的 Binder 时,则默认使用 DefaultModelBinder。默认情况下,DefaultModelBinder 从如下 4 种途径查找要绑定到 Model 上的值:

  1. Request.Form,HTML form 元素提供的值。
  2. RouteData.Values,通过应用程序路由提供的值。
  3. Request.QueryString,所请求 URL 的 query string 值。
  4. Request.Files,客户端上传的文件。

简单示例:用户登录支持全角

Controller

public class LoginController : Controller
    {
        // GET: Login
        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Index(string name ,string password)
        {
            if (name.Equals("Admin",StringComparison.OrdinalIgnoreCase)&& password =="123")
            {
                return Content("OK");
            }
            return Content("Error");
        }
    }

View:

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div> 
        <form action="~/Login/Index" method="post">
            <input type="text" name="name" placeholder="用户名"/> <br/>
            <input type="text" name="password" placeholder="密码"/> <br />
            <input type="submit"  value="登录" />
        </form>
    </div>
</body>
</html>

ModelBinder:

public class LoginModelbinder : DefaultModelBinder
    {
        public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            var values = base.BindModel(controllerContext, bindingContext);
            if (values is string)
            {
                return ToDBC(values.ToString().Trim());
            }
            return values;
        }
                                            
        ///转全角的函数(SBC case)
        ///全角空格为12288,半角空格为32
        ///其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248
        public static string ToSBC(string input)
        {
            // 半角转全角:
            char[] array = input.ToCharArray();
            for (int i = 0; i < array.Length; i++)
            {
                if (array[i] == 32)
                {
                    array[i] = (char)12288;
                    continue;
                }
                if (array[i] < 127)
                {
                    array[i] = (char)(array[i] + 65248);
                }
            }
            return new string(array);
        }

        ///转半角的函数(DBC case)
        ///全角空格为12288,半角空格为32
        ///其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248// 
        public static string ToDBC(string input)
        {
            char[] array = input.ToCharArray();
            for (int i = 0; i < array.Length; i++)
            {
                if (array[i] == 12288)
                {
                    array[i] = (char)32;
                    continue;
                }
                if (array[i] > 65280 && array[i] < 65375)
                {
                    array[i] = (char)(array[i] - 65248);
                }
            }
            return new string(array);

        }
    }

不要忘了 Global.asax中注册

 public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            ModelBinders.Binders.Add( typeof(string),new LoginModelbinder());
        }
    }

看看效果.

.NET  MVC  ModelBinder基本使用

 .NET  MVC  ModelBinder基本使用

 .NET  MVC  ModelBinder基本使用