1

I have a mvc5 webpage that has a form method that when the checkbox is checked submits the form and sends the get request. The request header submits the CarNumber and CarId as expected but the checkbox value is sent twice in the header why is this happening? How can I fix the issue.

Header value repeated like this

ShowAllDesignReviews=true&ShowAllDesignReviews=false


@using (Html.BeginForm("Index", "DesignReview", FormMethod.Get))
 {

@Html.Hidden("CarNumber", "CarNumber")
@Html.Hidden("CarId", "CarId")
@Html.CheckBoxFor(model => Model.ShowAllDesignReviews, new { @onchange = "this.form.submit()" })

}
tereško
  • 58,060
  • 25
  • 98
  • 150
Bob
  • 1,065
  • 2
  • 16
  • 36

2 Answers2

1

This is how CheckBoxFor helper in MVC generates the html, for easy model binding

<input name="ShowAllDesignReviews" type="checkbox" value="true" />
<input name="ShowAllDesignReviews" type="hidden" value="false" /> 

If you dont select check box, the field will not be posted. To make the value(false) to be passed they used Hidden.

Refer asp.net mvc: why is Html.CheckBox generating an additional hidden input

Better to have your controller that accepts the model

public ActionResult Save(Design model)
{
var boolSet=model.ShowAllDesignReviews;
}
Community
  • 1
  • 1
Murali Murugesan
  • 22,423
  • 17
  • 73
  • 120
  • So how should I change the razor or should I use a radio button? as Jeroen suggested – Bob Nov 19 '13 at 15:16
  • No need to change anything in razor. Change your controller code that looks from `Request` and do with model – Murali Murugesan Nov 19 '13 at 15:18
  • As Murali suggests, make use of the modelbinder in MVC. It works brilliantly, saves you from having to type a lot get/set code, and it handles these MVC quirks like double value sending without a hitch. – Flater Nov 19 '13 at 15:37
  • well using the model bind works but it still displays the odd header – Bob Nov 19 '13 at 16:33
  • @Bob, what you are getting displayed? Add your view code also – Murali Murugesan Nov 19 '13 at 16:36
0

See Murali's answer as to why this happens. I had some similar scenarios where I wanted to have a "standard" checkbox, so I wrote the following HtmlHelper:

public static class StandardCheckboxForHelper
{
    public static MvcHtmlString StandardCheckboxFor<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> propertyExpression)
    {
        return html.StandardCheckboxFor(propertyExpression, null);
    }

    public static MvcHtmlString StandardCheckboxFor<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> propertyExpression, object htmlAttributes)
    {
        return html.StandardCheckboxFor(propertyExpression, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
    }


    public static MvcHtmlString StandardCheckboxFor<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> propertyExpression, IDictionary<string, object> htmlAttributes)
    {
        var propertyName = html.NameFor(propertyExpression).ToString();
        var propertyId = html.IdFor(propertyExpression).ToString();
        var propertyValueString = html.ValueFor(propertyExpression).ToString();
        var propertyValue = ModelMetadata.FromLambdaExpression(propertyExpression, html.ViewData).Model;
        var unobtrusiveValidationAttributes = html.GetUnobtrusiveValidationAttributes(propertyName, ModelMetadata.FromLambdaExpression(propertyExpression, html.ViewData));

        var checkbox = new TagBuilder("input");
        checkbox.MergeAttribute("type", "checkbox");
        checkbox.MergeAttribute("id", propertyId);
        checkbox.MergeAttribute("name", propertyName);
        checkbox.MergeAttributes(unobtrusiveValidationAttributes);
        checkbox.MergeAttributes(htmlAttributes);
        bool propertyValueConverted;
        if ((propertyValue is bool) && (bool) propertyValue)
            checkbox.MergeAttribute("checked", "checked");
        else if ((propertyValue is bool?) && ((bool?)propertyValue).GetValueOrDefault(false))
            checkbox.MergeAttribute("checked", "checked");
        else if (bool.TryParse(propertyValueString, out propertyValueConverted) && propertyValueConverted)
            checkbox.MergeAttribute("checked", "checked");
        return checkbox.ToHtml();
    }
}

Usage is the same as the CheckboxFor helper, and it will only render one simple checkbox.

Moeri
  • 9,104
  • 5
  • 43
  • 56