的WebGrid
分页链接在所有情况下正常工作,除了一个(我发现)。
WebGrid
pagination links work properly in all cases, except one (that I noticed).
在使用 CheckBoxFor
MVC中,它创建了一个输入[类型=隐藏]
和输入[类型=复选框]
为同一字段,以便它可以处理的状态。所以,如果你有一个名为 X
字段,并在 GET
方法提交表单,你最终会与像这样的网址:
When you use CheckBoxFor
in MVC, it creates an input[type=hidden]
and an input[type=check-box]
for the same field so that it can handle state. So, if you have a field named X
and submit your form in the GET
method, you will end up with an URL like this:
http://foo.com?X=false&X=true
默认的模型粘合剂可以了解这些多个实例OS X
并计算出它的价值。
The default model binder can understand these multiple instances os X
and figure out it's value.
当您尝试在分页的WebGrid
将出现问题。它的行为是试图抓住当前请求的参数,并再经过他们在分页链接。然而,由于有多个 X
,它会通过 X =假,真
而不是预期的 X =假
或 X =假放; X =真
The problem occurs when you try to paginate the WebGrid
. It's behavior is to try catch the current request parameters and repass them in the pagination links. However, as there's more than one X
, it will pass X=false,true
instead of the expected X=false
or X=false&X=true
这是一个问题,因为 X =假,真
将无法正确绑定。它会在模型的粘合剂,现有的行动的一开始就触发一个例外。
That's a problem because X=false,true
won't bind properly. It will trigger an exception in the model binder, prior to the beggining of the action.
有没有一种办法可以解决呢?
Is there a way I can solve it?
编辑:
这似乎是一个很具体的问题,但事实并非如此。近一个复选框,每一个搜索形式将打破的WebGrid分页。 (如果您使用的是GET)
It seems like a very specific problem but it's not. Nearly every search form with a check box will break the WebGrid pagination. (If you are using GET)
编辑2:
我想我只有2个选项是:
I think my only 2 options are:
在建立自己的WebGrid寻呼机是在传递参数分页链接更聪明 在建立自己的布尔模型绑定的理解假的,真正的
为有效
Build my own WebGrid pager that is more clever on passing parameters for pagination links
Build my own Boolean model binder that understands false,true
as valid
如果别人从描述的问题的痛苦,你可以解决此使用自定义的模型绑定是这样的:
In case anybody else is suffering from the issue described, you can work around this using a custom model binder like this:
public class WebgridCheckboxWorkaroundModelBinder : DefaultModelBinder
{
protected override void BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, System.ComponentModel.PropertyDescriptor propertyDescriptor)
{
if (propertyDescriptor.PropertyType == typeof (Boolean))
{
var value = bindingContext.ValueProvider.GetValue(propertyDescriptor.Name);
if (value.AttemptedValue == "true,false")
{
PropertyInfo prop = bindingContext.Model.GetType().GetProperty(propertyDescriptor.Name, BindingFlags.Public | BindingFlags.Instance);
if (null != prop && prop.CanWrite)
{
prop.SetValue(bindingContext.Model, true, null);
}
return;
}
}
base.BindProperty(controllerContext, bindingContext, propertyDescriptor);
}
}