在barkets与(属性)方法的参数 [英] Method parameter with (attribute) in barkets
问题描述
我有一个KedoUI code例子。
I have a code example from KedoUI.
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,
...
});
}
这个例子正常工作。
我在困惑 [DataSourceRequest]
Customers_Read
方法...
I am confused about [DataSourceRequest]
in Customers_Read
method...
当我删除(?属性?) [DataSourceRequest]
,从请求属性为空(NULL)......(他们不绑定) - >结果:过滤器不工作。
When i remove (the ?Attribute?) [DataSourceRequest]
, the properties from request are empty(null)... (they aren't bound) -> result: filters doesn't work ..
什么的 [DataSourceRequest]
?这是像性能属性?
What does the [DataSourceRequest]
? is this like an attribute on properties?
code示例 - > IndexController.cs
code示例
Code Example -> IndexController.cs Code Example
推荐答案
你所看到的是一个模型绑定属性。在 DataSourceRequest
实际上 DataSourceRequestAttribute
,延长<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.mvc.custommodelbinderattribute(v=vs.118).aspx\"相对=nofollow> CustomModelBinderAttribute
类。创建这样一个属性是相当简单:
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();
}
}
在自定义绑定可以通过创建一个简单的的ActionResult
和查询字符串参数调用它(因为我上面的实现查找参数在查询字符串)进行测试
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>
由于DavidG指出,在 DataSourceRequestAttribute
比 DataSourceRequest
不同。他们似乎有因属性
命名规则,即 DataSourceRequestAttribute
的同名失去了属性
装饰对象或物业时的一部分。
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.
作为一个结论,在 DataSourceRequestAttribute
只是告诉框架,一个自定义模型粘合剂(大概 DataSourceRequestModelBinder
或类似的东西)应该被用于 DataSourceRequest要求
参数。
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.
欲了解更多信息,以下链接:源,< A HREF =http://gregbee.ch/blog/deserializing-the-request-body-into-a-parameter-with-asp-net-mvc相对=nofollow>来源。
Please see the following links for additional information: source, source.
这篇关于在barkets与(属性)方法的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!