c# - 括号中带有(属性)的方法参数

标签 c# asp.net-mvc parameters kendo-ui arguments

我有一个来自 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] 时,请求中的属性为空(null)...(它们未绑定(bind))-> 结果:过滤器不起作用.

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

Code Example -> IndexController.cs 代码示例

最佳答案

您看到的是模型 Binder 属性。 DataSourceRequest 实际上是 DataSourceRequestAttribute 并扩展了 CustomModelBinderAttribute类(class)。创建这样的属性非常简单:

首先我们需要一个模型:

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

    public string MyProp2 { get; set; }
}

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

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;
     }
}

我们需要做的最后一件事是创建可以在操作结果签名中设置的模型绑定(bind)器属性。它的唯一目的是指定必须用于它装饰的参数的模型绑定(bind)器:

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

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

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 所指出的,DataSourceRequestAttribute 不同于 DataSourceRequest。由于 Attribute 命名约定,它们似乎具有相同的名称,即 DataSourceRequestAttribute 在装饰对象或属性时丢失了 Attribute 部分。

作为结论,DataSourceRequestAttribute 只是告诉框架,自定义模型绑定(bind)器(可能是 DataSourceRequestModelBinder 或类似的东西)应该用于 DataSourceRequest 请求 参数。

请参阅以下链接以获取更多信息:source , source .

关于c# - 括号中带有(属性)的方法参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26136758/

相关文章:

c# - 使用 Owin 和 Windows 身份验证在 MVC 5 应用程序中添加声明

asp.net - 构建asp.net mvc网站时出错

postgresql - 如何调用 TimescaleDB 中的过程?

c - 什么是参数前向声明?

c# - TPL Dataflow BufferBlock 线程安全吗?

c# - 在 .NET 4.5.1 中设置 ASP.NET Identity ConnectionString 属性

c# - Azure AD Graph API - 使用 C# 将用户分配给应用程序

javascript - 在线测试系统asp.net mvc

asp.net-mvc - 构建中等大小的 asp mvc - 使用 ninject 和创建对象

python - Pyspark:将参数传递给数据帧中的字符串列