ASP.NET MVC Model绑定(六)

ASP.NET MVC Model绑定(六)

前言

前面的篇幅对于IValueProvider的使用作个基础的示例讲解,可是没并无对 IValueProvider类型的实现作详细的介绍,然而MVC框架中给咱们提供了几种默认的实现类型,在本篇中将会对NameValueCollectionValueProvider类型作一个示例讲解,了解一下MVC框架给咱们提供的值提供程序是怎么处理Model值的。框架

 

Model绑定

  • IModelBinder、自定义Model绑定器简单实现
  • Model绑定器在MVC框架中的位置
  • MVC中的默认Model绑定器生成过程
  • IModelBinderProvider的简单应用
  • IValueProvider在MVC框架中生成的位置以及过程
  • IValueProvider的应用场景
  • IValueProvider的实现之NameValueCollectionValueProvider

IValueProvider的实现之NameValueCollectionValueProvider

前面的一篇中咱们对IValueProvider的使用做了示例演示,那是一个从控制器方法到视图的一个绑定的过程,你们有没有想过在视图里的数据是怎么在绑定回控制器部分的。视图中的数据类型的不一样对应的使用绑定的类型也不一样,本篇就为你们示例一个自定义类型的绑定。ide

代码1-1函数

public class Customer
    {
        [HiddenInput(DisplayValue=true)]
        public string CustomerID { get; set; }

        [Display(Name="姓名")]
        public string Name { get; set; }

        [DataType(DataType.Date)]
        [Display(Name="注册日期")]
        public DateTime RegistrationDate{ get; set; }

        [UIHint("Address")]
        public Address Address { get; set; } 
    }
    public class Address
    {
        [Display(Name="地址名称")]
        [MyCustomMetadataAware]
        public string AddressName { get; set; }
    }

对的代码1-1中的类型已经出现过不少次了,可是出于对没看过前面篇幅的朋友负责的态度也要加上阿,这是下面示例要用到的示例ViewModel。测试

首先咱们须要数据展现:spa

代码1-2:code

    public class ValueProviderCaseController : Controller
    {
        public ViewResult Show(Customer customer)
        {
            return View(customer);
        }

    }

代码1-2中定义了个Show()方法,参数类型为代码1-1所示类型。orm

看下Show()方法对应的视图,固然了这样建立的是强类型视图,代码1-3.blog

代码1-3get

@model ConsoleApplication2.Customer
@{
    ViewBag.Title = "Show";  
}
<h2>
    Show</h2>
@using (Html.BeginForm("Update", "ValueProviderCase"))
{
    <p>@Html.EditorForModel()</p>
    <p>@Html.EditorFor(m => Model.Address)</p>
    <br />
    <input type="submit" value="提交" />
}

在代码1-3中,咱们也看到了,使用了BeginForm()视图辅助器,而且令表单指向ValueProviderCase 控制器的Update()方法,这个后面会说到,暂且带过。如今这个时候咱们还运行不了项目,咱们须要为代码1-2中的Show()配置一个Model绑定器,代码1-4.input

代码1-4

 public class MyCustomModelBinder : IModelBinder
    {
        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {

            if (controllerContext.HttpContext.Request.RequestType == "GET")
            {
                return new Customer()
                {
                    CustomerID = "010",
                    Name = "测试人员",
                    RegistrationDate = DateTime.Now,
                    Address = new Address()
                    {
                        AddressName = "天空之城"
                    }
                };
            }
            return null;
        }
    }

从代码1-4中,咱们能够看到对Model绑定器作了控制,使它在请求类型为Get的时候返回代码1-1所示类型的ViewModel实例。由于后面的示例咱们也还会用到这个Model绑定器,因此加了控制。对于Model绑定器的注册这里就不说了,运行结果如图1.

图1

若是这个时候咱们单击提交按钮会把数据会变成什么样子呢?数据到了当前系统上下文中。

 

NameValueCollection

为何要讲到NameValueCollection类型呢,由于NameValueCollectionValueProvider类型中的操做就是针对的NameValueCollection类型的,这里咱们来看图1中点击提交后的的数据展现如图2

图2

说好了数据呢?你们别急,图2中的是NameValueCollection类型的AllKeys属性中的值,而NameValueCollection类型的实例是经过controllerContext.HttpContext.Request.Form这样获取而来,也就是上面说到的点击“提交”后所造成的数据类型。而咱们的NameValueCollectionValueProvider类型则是对NameValueCollection类型的处理,具体的内部处理细节就不在这详细描述了。

下面咱们须要作提交后的操做,就是显示到更新界面,那咱们得按照上面代码1-3中的定义的那样,须要个Update()方法,示例代码1-5.

代码1-5

    public class ValueProviderCaseController : Controller
    {

        public ViewResult Show(Customer customer)
        {
            return View(customer);
        }
        [HttpPost]
        public ActionResult Update(Customer customer)
        {
            return View(customer);
        }

    }

这个时候咱们是看不到绑定器内部的实现的,因此咱们来模拟一下,修改上面代码1-4中的内容,如示例代码1-6.

代码1-6

public class MyCustomModelBinder : IModelBinder
    {
        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {

            if (controllerContext.HttpContext.Request.RequestType == "GET")
            {
                return new Customer()
                {
                    CustomerID = "010",
                    Name = "测试人员",
                    RegistrationDate = DateTime.Now,
                    Address = new Address()
                    {
                        AddressName = "天空之城"
                    }
                };
            }
            else if (controllerContext.HttpContext.Request.RequestType == "POST")
            {

                Customer customer = new Customer();
                customer.Address = new Address();
                NameValueCollection nameValueCollection =
                    controllerContext.HttpContext.Request.Form;
                NameValueCollectionValueProvider nameValueCollectionValueProvider =
                    new NameValueCollectionValueProvider(
                        nameValueCollection,
                        System.Globalization.CultureInfo.InstalledUICulture);
                customer.CustomerID = GetValue(nameValueCollectionValueProvider, "CustomerID");
                customer.Name = GetValue(nameValueCollectionValueProvider, "Name");
                customer.RegistrationDate = DateTime.Parse(GetValue(nameValueCollectionValueProvider, "RegistrationDate"));
                customer.Address.AddressName = GetValue(nameValueCollectionValueProvider, "Address.AddressName");
                return customer;

            }
            return null;
        }

        private string GetValue(IValueProvider valueProvider, string preFix)
        {
            return valueProvider.ContainsPrefix(preFix) ? valueProvider.GetValue(preFix).AttemptedValue : null;
        }
    }

这里忘了说了,能够把NameValueCollection类型想象成一个键值队类型的集合,而且NameValueCollection类型的实例已经包含着全部数据了,可使用它内部的GetValue方法(并不是代码1-6中的GetValue方法)来获取所对应的值,在NameValueCollectionValueProvider类型内部也是使用的这个方法来获取的值。

在代码1-6中咱们对Model绑定器修改了太多了,首先是控制器了在请求类型为POST的时候(也就是为了在请求Update()方法时所用)使用这个Model绑定器,随之咱们实例化了一个代码1-1中所示的ViewModel实例,后面会对它进行赋值,随后咱们经过上下文获取到表单中的数据(NameValueCollection类型的实例)做为NameValueCollectionValueProvider类型构造函数的参数,咱们还在Model绑定器中定义了个私有的GetValue()方法,这个的用途就是根据执行的前缀(NameValueCollection类型中的键值,也就是视图元素中的Name属性)从NameValueCollectionValueProvider类型的实例中获取对应的数据。

如今看一下Update()方法所对应的视图代码,示例代码1-7

@model ConsoleApplication2.Customer
@{
    ViewBag.Title = "Update";
}
<h2>
    Update</h2>
<p>@Html.EditorForModel()</p>
<p>@Html.EditorFor(m => Model.Address)</p>

这个时候咱们能够运行项目,首先看到Show页面后,修改其中的值,而后提交事后会看到修改的值已经更新到了Update的界面中。

 

 

做者:金源

出处:http://www.cnblogs.com/jin-yuan/

本文版权归做者和博客园共有,欢迎转载,但未经做者赞成必须保留此段声明,且在文章页面

相关文章
相关标签/搜索