Asp .net mvc 3 CheckBoxFor方法输出隐藏字段,当选中的复选框为true时,隐藏字段值为false

时间:2012-06-13 08:50:47

标签: asp.net-mvc checkboxfor

CheckBoxFor(t => t.boolValue,new {disabled =“disabled”})方法,以禁用模式呈现复选框。

该方法也会渲染隐藏字段。

我的问题是为什么此隐藏字段对于禁用复选框具有错误值? 我相信隐藏字段的目的是在默认复选框行为

上有一些额外的行为

有没有办法覆盖默认的MVC功能,以便隐藏该字段的值 是否基于复选框的状态,即使在禁用模式下?

4 个答案:

答案 0 :(得分:14)

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

这就是说,而不是在复选框上使用disabled="disabled"使用readonly="readonly"。这样,您将保持用户无法修改其值所需的相同行为,但除此之外,它将在提交表单时将其值发送到服务器:

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

更新:

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

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

更新2:

这是一个带有额外隐藏字段的完整测试用例。

型号:

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)。

答案 1 :(得分:3)

我发现查询字符串中添加的参数看起来很乱,让人们认为我们作为开发人员做错了什么。所以我采用了创建自己的InputExtensions类的极端方法,它允许我决定是否要渲染隐藏的输入。

以下是我创建的InputExtensions类(基于现有的MVC代码以保持完全兼容性):

public static class InputExtensions
{
    //
    // Checkboxes
    //

    public static MvcHtmlString CheckBoxFor<TModel>(this HtmlHelper<TModel> htmlHelper,
        Expression<Func<TModel, bool>> expression, bool renderHiddenInput)
    {
        if (renderHiddenInput)
        {
            return System.Web.Mvc.Html.InputExtensions.CheckBoxFor(htmlHelper, expression);
        }
        return CheckBoxFor(htmlHelper, expression, false);
    }

    public static MvcHtmlString CheckBoxFor<TModel>(this HtmlHelper<TModel> htmlHelper,
        Expression<Func<TModel, bool>> expression, object htmlAttributes, bool renderHiddenInput)
    {
        if (renderHiddenInput)
        {
            return System.Web.Mvc.Html.InputExtensions.CheckBoxFor(htmlHelper, expression, htmlAttributes);
        }
        return CheckBoxFor(htmlHelper, expression, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes), false);
    }

    public static MvcHtmlString CheckBoxFor<TModel>(this HtmlHelper<TModel> htmlHelper,
        Expression<Func<TModel, bool>> expression, IDictionary<string, object> htmlAttributes,
        bool renderHiddenInput)
    {
        if (renderHiddenInput)
        {
            return System.Web.Mvc.Html.InputExtensions.CheckBoxFor(htmlHelper, expression, htmlAttributes);
        }

        if (expression == null)
        {
            throw new ArgumentNullException("expression");
        }

        ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
        bool? isChecked = null;
        if (metadata.Model != null)
        {
            bool modelChecked;
            if (Boolean.TryParse(metadata.Model.ToString(), out modelChecked))
            {
                isChecked = modelChecked;
            }
        }

        return CheckBoxHelper(htmlHelper, metadata, ExpressionHelper.GetExpressionText(expression), isChecked, htmlAttributes);
    }

    private static MvcHtmlString CheckBoxHelper(HtmlHelper htmlHelper, ModelMetadata metadata, string name, bool? isChecked, IDictionary<string, object> htmlAttributes)
    {
        RouteValueDictionary attributes = ToRouteValueDictionary(htmlAttributes);

        bool explicitValue = isChecked.HasValue;
        if (explicitValue)
        {
            attributes.Remove("checked"); // Explicit value must override dictionary
        }

        return InputHelper(htmlHelper,
                           InputType.CheckBox,
                           metadata,
                           name,
                           value: "true",
                           useViewData: !explicitValue,
                           isChecked: isChecked ?? false,
                           setId: true,
                           isExplicitValue: false,
                           format: null,
                           htmlAttributes: attributes);
    }

    //
    // Helper methods
    //

    private static MvcHtmlString InputHelper(HtmlHelper htmlHelper, InputType inputType, ModelMetadata metadata, string name, object value, bool useViewData, bool isChecked, bool setId, bool isExplicitValue, string format, IDictionary<string, object> htmlAttributes)
    {
        string fullName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name);
        if (string.IsNullOrEmpty(fullName))
        {
            throw new ArgumentException("Value cannot be null or empty.", "name");
        }

        var tagBuilder = new TagBuilder("input");
        tagBuilder.MergeAttributes(htmlAttributes);
        tagBuilder.MergeAttribute("type", HtmlHelper.GetInputTypeString(inputType));
        tagBuilder.MergeAttribute("name", fullName, true);

        string valueParameter = htmlHelper.FormatValue(value, format);
        var usedModelState = false;

        bool? modelStateWasChecked = GetModelStateValue(htmlHelper.ViewData, fullName, typeof(bool)) as bool?;
        if (modelStateWasChecked.HasValue)
        {
            isChecked = modelStateWasChecked.Value;
            usedModelState = true;
        }
        if (!usedModelState)
        {
            string modelStateValue = GetModelStateValue(htmlHelper.ViewData, fullName, typeof(string)) as string;
            if (modelStateValue != null)
            {
                isChecked = string.Equals(modelStateValue, valueParameter, StringComparison.Ordinal);
                usedModelState = true;
            }
        }
        if (!usedModelState && useViewData)
        {
            isChecked = EvalBoolean(htmlHelper.ViewData, fullName);
        }
        if (isChecked)
        {
            tagBuilder.MergeAttribute("checked", "checked");
        }
        tagBuilder.MergeAttribute("value", valueParameter, isExplicitValue);

        if (setId)
        {
            tagBuilder.GenerateId(fullName);
        }

        // If there are any errors for a named field, we add the css attribute.
        ModelState modelState;
        if (htmlHelper.ViewData.ModelState.TryGetValue(fullName, out modelState))
        {
            if (modelState.Errors.Count > 0)
            {
                tagBuilder.AddCssClass(HtmlHelper.ValidationInputCssClassName);
            }
        }

        tagBuilder.MergeAttributes(htmlHelper.GetUnobtrusiveValidationAttributes(name, metadata));

        return MvcHtmlString.Create(tagBuilder.ToString(TagRenderMode.SelfClosing));
    }

    private static RouteValueDictionary ToRouteValueDictionary(IDictionary<string, object> dictionary)
    {
        return dictionary == null ? new RouteValueDictionary() : new RouteValueDictionary(dictionary);
    }

    private static object GetModelStateValue(ViewDataDictionary viewData, string key, Type destinationType)
    {
        ModelState modelState;
        if (viewData.ModelState.TryGetValue(key, out modelState))
        {
            if (modelState.Value != null)
            {
                return modelState.Value.ConvertTo(destinationType, culture: null);
            }
        }
        return null;
    }

    private static bool EvalBoolean(ViewDataDictionary viewData, string key)
    {
        return Convert.ToBoolean(viewData.Eval(key), CultureInfo.InvariantCulture);
    }
}

然后你可以像这样调用方法:

@Html.CheckBoxFor(t => t.boolValue, new { disabled="disabled" }, false)

答案 2 :(得分:2)

以下是我发现的快速提示:如果为对象上的布尔值设置了默认值。可以使用值为true / false来写出标记(此值应该与对象上的默认布尔值相反)

<input type="checkbox" name="IsAllDayEvent" value="true" id="IsAllDayEvent" />

仅在选中复选框时才会发送该值,因此它可以正常工作。

答案 3 :(得分:0)

尽管Darin的答案非常好,但还有另一种方法可以确保禁用的复选框按预期发布回复。

此解决方案也适用于根据模型中的属性有条件禁用复选框的情况,或基于用户选择或客户端检索数据启用/禁用客户端的情况。

您可以保持视图简单:(此处禁用设置是可选的)

@Html.CheckBoxFor(m => m.Something, new { id = "somethingCheckbox", disabled = "disabled" })

正如Darin所提到的那样生成一个额外的隐藏输入字段设置为false,因此当未选中复选框时,复选框值也会回发。因此,我们知道Something字段将始终回发,无论是否选中复选框,是否禁用。唯一的问题是,如果禁用复选框,它将始终回发为False

我们可以在提交之前通过一些javaScript后期处理来解决这个问题。在我的例子中,代码看起来像这样:

var dataToPost = $form.serializeArray();

if (dataToPost != null) {
    for (var i = 0; i < dataToPost.length; i++) {
        if (dataToPost[i].name === "Something") {
            dataToPost[i].value = $("#somethingCheckbox").attr("checked") === "checked";
            }
        }
    }  
$.post($form.attr("action"), dataToPost)

您可能需要根据您的方案对其进行修改,但您应该明白这一点。

相关问题