的ASP.NET MVC 3 CheckBoxFor方法输出隐藏字段,当复选框被与选定的真禁用的隐藏字段值是假 [英] Asp .net mvc 3 CheckBoxFor method outputs hidden field, that hidden field value is false when checkbox is disabled with selected true

查看:130
本文介绍了的ASP.NET MVC 3 CheckBoxFor方法输出隐藏字段,当复选框被与选定的真禁用的隐藏字段值是假的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CheckBoxFor(t => t.boolValue,新的{disabled ="disabled"})方法,用于在禁用模式下呈现复选框.

CheckBoxFor(t => t.boolValue, new { disabled="disabled" }) method to render a checkbox, in disabled mode.

该方法还呈现一个隐藏字段.

The method renders a hidden field as well.

我的问题是,为什么此隐藏字段的禁用复选框具有错误的值? 我相信隐藏字段的目的是在默认复选框行为之外具有一些额外的行为

My question is why does this hidden field has a false value for disabled check box? I believe the purpose of the hidden field is to have some extra behavior over the default check box behavior

是否有一种方法可以覆盖默认的MVC功能,以便此隐藏字段的值 甚至在禁用模式下也基于复选框的状态?

Is there a way to override default MVC functionality so that the value of this hidden field is based on the state of the checkbox even in disabled mode?

推荐答案

隐藏字段用于将复选框值绑定到布尔属性.问题是,如果未选中此复选框,则不会将任何内容发送到服务器,因此ASP.NET MVC使用此隐藏字段发送false并绑定到相应的布尔字段.除了编写自定义帮助程序外,您无法修改此行为.

The hidden field is used to bind the checkbox value to a boolean property. The thing is that if a checkbox is not checked, nothing is sent to the server, so ASP.NET MVC uses this hidden field to send false and bind to the corresponding boolean field. You cannot modify this behavior other than writing a custom helper.

这就是说,而不是使用disabled="disabled",请在复选框上使用readonly="readonly".这样,您将保留用户无法修改其值的期望行为,但是除了在提交表单时将其值发送到服务器之外,还可以:

This being said, instead of using disabled="disabled" use readonly="readonly" on the checkbox. This way you will keep the same desired behavior that the user cannot modify its value but in addition to that its value will be sent to the server when the form is submitted:

@Html.CheckBoxFor(x => x.Something, new { @readonly = "readonly" })


更新:


UPDATE:

正如评论部分所指出的,readonly属性不适用于Google Chrome.另一种可能性是使用另一个隐藏字段并禁用该复选框:

As pointed out in the comments section the readonly attribute doesn't work with Google Chrome. Another possibility is to use yet another hidden field and disable the checkbox:

@Html.HiddenFor(x => x.Something)
@Html.CheckBoxFor(x => x.Something, new { disabled = "disabled" })


更新2:


UPDATE 2:

这是一个带有附加隐藏字段的完整测试用例.

Here's a full testcase with the additional hidden field.

型号:

public class MyViewModel
{
    public bool Foo { get; set; }
}

控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View(new MyViewModel { Foo = true });
    }

    [HttpPost]
    public ActionResult Index(MyViewModel model)
    {
        return Content(model.Foo.ToString());
    }
}

查看:

@model MyViewModel

@using (Html.BeginForm())
{
    @Html.HiddenFor(x => x.Foo)
    @Html.CheckBoxFor(x => x.Foo, new { disabled = "disabled" })
    <button type="submit">OK</button>
}

提交表单时,Foo属性的值为true.我已经在所有主流浏览器(Chrome,FF,IE)上进行了测试.

When the form is submitted the value of the Foo property is true. I have tested with all major browsers (Chrome, FF, IE).

这篇关于的ASP.NET MVC 3 CheckBoxFor方法输出隐藏字段,当复选框被与选定的真禁用的隐藏字段值是假的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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