方括号中带有(属性)的方法参数 [英] Method parameter with (attribute) in brackets

查看:257
本文介绍了方括号中带有(属性)的方法参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个KendoUI的代码示例.

public ActionResult Customers_Read([DataSourceRequest]DataSourceRequest request)
{
    return Json(GetCustomers().ToDataSourceResult(request));
}

private static IEnumerable<CustomerViewModel> GetCustomers()
{
    var northwind = new SampleEntities();
    return northwind.Customers.Select(customer => 
        new CustomerViewModel
        {
            CustomerID  = customer.CustomerID,
            CompanyName = customer.CompanyName,
            ContactName = customer.ContactName,
            // ...
        });
}

此示例工作正常.

我对Customers_Read方法中的[DataSourceRequest]感到困惑...

当我删除(属性?)[DataSourceRequest]时,请求中的属性为空(空)...(它们没有绑定)->结果:过滤器不起作用.

什么是[DataSourceRequest]?就像属性上的属性吗?

代码示例-> IndexController.cs 代码示例

解决方案

您看到的是模型联编程序属性. DataSourceRequest实际上是DataSourceRequestAttribute,并且扩展了 CustomModelBinderAttribute 类.创建这样的属性非常简单:

首先,我们需要一个模型:

public class MyModel
{
    public string MyProp1 { get; set; }

    public string MyProp2 { get; set; }
}

我们需要能够通过创建自定义模型联编程序来创建绑定.根据您将值发送到服务器的方式,从表单或查询字符串中获取值:

public class MyModelBinder : IModelBinder
{
     public object BindModel (ControllerContext controllerContext, ModelBindingContext bindingContext)
     {
         MyModel model = new MyModel();

         //model.MyProp1 = controllerContext.HttpContext.Request.Form["MyProp1"];
         //model.MyProp2 = controllerContext.HttpContext.Request.Form["MyProp2"];
         //or
         model.MyProp1 = controllerContext.HttpContext.Request.QueryString["MyProp1"];
         model.MyProp2 = controllerContext.HttpContext.Request.QueryString["MyProp2"];

         return model;
     }
}

我们要做的最后一件事是创建模型绑定器属性,该属性可以在动作结果签名中设置.其唯一目的是指定模型装订器,该装订器必须用于其装饰的参数:

public class MyModelBinderAttribute : CustomModelBinderAttribute
{
     public override IModelBinder GetBinder()
     {
          return new MyModelBinder();
     }
}

可以通过创建一个简单的ActionResult并使用查询字符串中的参数调用自定义绑定来进行测试(因为上面的实现在查询字符串中查找了参数):

public ActionResult DoBinding([MyModelBinder]MyModel myModel)
{
    return new EmptyResult();
}

//inside the view
<a href="/Home/DoBinding?MyProp1=value1&MyProp2=value2">Click to test</a>

正如DavidG所指出的,DataSourceRequestAttributeDataSourceRequest不同.由于Attribute名称约定,它们似乎具有相同的名称,即DataSourceRequestAttribute在装饰对象或属性时会松开Attribute部分.

作为结论,DataSourceRequestAttribute只是告诉框架DataSourceRequest request参数应使用自定义模型绑定程序(可能是DataSourceRequestModelBinder或类似名称).

有关其他信息,请参见以下链接:,<一个href ="http://gregbee.ch/blog/deserializing-the-request-body-into-a-parameter-with-asp-net-mvc" rel ="nofollow">源.

I have a code example from KendoUI.

public ActionResult Customers_Read([DataSourceRequest]DataSourceRequest request)
{
    return Json(GetCustomers().ToDataSourceResult(request));
}

private static IEnumerable<CustomerViewModel> GetCustomers()
{
    var northwind = new SampleEntities();
    return northwind.Customers.Select(customer => 
        new CustomerViewModel
        {
            CustomerID  = customer.CustomerID,
            CompanyName = customer.CompanyName,
            ContactName = customer.ContactName,
            // ...
        });
}

This example works fine.

I am confused about [DataSourceRequest] in Customers_Read method...

When I remove (the Attribute?) [DataSourceRequest], the properties from request are empty (null)... (they aren't bound) -> result: filters doesn't work.

What is the [DataSourceRequest]? Is it like an attribute on properties?

Code Example -> IndexController.cs Code Example

解决方案

What you are seeing is a model binder attribute. The DataSourceRequest is actually DataSourceRequestAttribute and extends the CustomModelBinderAttribute class. Creating such an attribute is fairly simple:

First we need a model:

public class MyModel
{
    public string MyProp1 { get; set; }

    public string MyProp2 { get; set; }
}

We need to be able to create the binding, by creating a custom model binder. Depending on how your values are sent to the server, get the values either from the form or the query string:

public class MyModelBinder : IModelBinder
{
     public object BindModel (ControllerContext controllerContext, ModelBindingContext bindingContext)
     {
         MyModel model = new MyModel();

         //model.MyProp1 = controllerContext.HttpContext.Request.Form["MyProp1"];
         //model.MyProp2 = controllerContext.HttpContext.Request.Form["MyProp2"];
         //or
         model.MyProp1 = controllerContext.HttpContext.Request.QueryString["MyProp1"];
         model.MyProp2 = controllerContext.HttpContext.Request.QueryString["MyProp2"];

         return model;
     }
}

The last thing we need to do is create the model binder attribute which can be set inside the action result signature. Its sole purpose is specifying the model binder which must be used for the parameter it decorates:

public class MyModelBinderAttribute : CustomModelBinderAttribute
{
     public override IModelBinder GetBinder()
     {
          return new MyModelBinder();
     }
}

The custom binding can be tested by creating a simple ActionResult and calling it with the parameters in the query string (since my implementation above looks for parameters in the query string):

public ActionResult DoBinding([MyModelBinder]MyModel myModel)
{
    return new EmptyResult();
}

//inside the view
<a href="/Home/DoBinding?MyProp1=value1&MyProp2=value2">Click to test</a>

As DavidG noted, the DataSourceRequestAttribute is different than DataSourceRequest. They appear to have the same names due to the Attribute name convention, i.e. DataSourceRequestAttribute looses the Attribute part when decorating an object or a property.

As a conclusion, the DataSourceRequestAttribute just tells the framework that a custom model binder (probably DataSourceRequestModelBinder or something similar) should be used for the DataSourceRequest request parameter.

Please see the following links for additional information: source, source.

这篇关于方括号中带有(属性)的方法参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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