如何在ASP.NET MVC Preview 5中使用新的ModelBinder类 [英] How do you use the new ModelBinder classes in ASP.NET MVC Preview 5

查看:47
本文介绍了如何在ASP.NET MVC Preview 5中使用新的ModelBinder类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您会注意到,预览5在其发行说明中包括以下内容:

You'll notice that Preview 5 includes the following in their release notes:

添加了对自定义模型联编程序的支持.自定义联编程序允许您将复杂类型定义为操作方法的参数.要使用此功能,请用[ModelBinder(…)]标记复杂类型或参数声明.

Added support for custom model binders. Custom binders allow you to define complex types as parameters to an action method. To use this feature, mark the complex type or the parameter declaration with [ModelBinder(…)].

那么您如何实际使用此功能,以便我可以在Controller中进行类似的工作?

So how do you go about actually using this facility so that I can have something like this work in my Controller:

public ActionResult Insert(Contact contact)
{
    if (this.ViewData.ModelState.IsValid)
    {
        this.contactService.SaveContact(contact);

        return this.RedirectToAction("Details", new { id = contact.ID}
    }
}

推荐答案

好吧,我对此进行了调查. ASP.NET为注册IControlBinders的实现提供了一个公共位置.他们还通过新的Controller.UpdateModel方法了解了此工作的基础.

Well I looked into this. ASP.NET provides a common location for registering the implementation of IControlBinders. They also have the basics of this working via the new Controller.UpdateModel method.

因此,我实质上通过创建一个IModelBinder实现来结合这两个概念,该实现对modelClass的所有公共属性执行与Controller.UpdateModel相同的功能.

So I essentially combined these two concepts by creating an implementation of IModelBinder that does the same thing as Controller.UpdateModel for all public properties of the modelClass.

public class ModelBinder : IModelBinder 
{
    public object GetValue(ControllerContext controllerContext, string modelName, Type modelType, ModelStateDictionary modelState)
    {
        object model = Activator.CreateInstance(modelType);

        PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(model);
        foreach (PropertyDescriptor descriptor in properties)
        {
            string key = modelName + "." + descriptor.Name;
            object value = ModelBinders.GetBinder(descriptor.PropertyType).GetValue(controllerContext, key, descriptor.PropertyType, modelState);
            if (value != null)
            {
                try
                {
                    descriptor.SetValue(model, value);
                    continue;
                }
                catch
                {
                    string errorMessage = String.Format("The value '{0}' is invalid for property '{1}'.", value, key);
                    string attemptedValue = Convert.ToString(value);
                    modelState.AddModelError(key, attemptedValue, errorMessage);
                }
            }
        }

        return model;
    }
}

您需要在Global.asax.cs中添加以下内容:

In your Global.asax.cs you'd need to add something like this:

protected void Application_Start()
{
    ModelBinders.Binders.Add(typeof(Contact), new ModelBinder());

这篇关于如何在ASP.NET MVC Preview 5中使用新的ModelBinder类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆