1

I am using html.checkbox in my mvc appplication , it will generate the html like

<input checked="checked" id="cb17" name="reports" type="checkbox" value="sdf" />
<input name="reports" type="hidden" value="false" /> 

    <input checked="checked" id="cb18" name="reports" type="checkbox" value="ty" />
<input name="reports" type="hidden" value="false" />

when the form gets posted it is sending all the values of html.checkbox to mvc action through model binding concept.

reports[0]-"sdf"
reports[1]-"false"
reports[2]="ty"
reports[3]="false" 

model:

 public IEnumerable<string> reports{ get; set; }

i knew html.checkbox will create the hidden fields internally, but when i posting the form it need to send either one value of checkbox. why it is sending hidden fields too. how to avoid this.

referred the below link

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

but not able to get the proper understanding.

8
  • The CheckBox() method is for binding to a booean property, not a string (it correctly generates a <input type="checkbox" ... value="true" /> and <input type="hidden" ... value="false" /> which will correctly bind to a boolean property when the form is submitted. Do not use CheckBox() for binding to a string. You have not shown your model, or what your attempting to bind to, or the method your posting back to so its impossible to understand why you trying to use this hack. Commented Dec 20, 2015 at 0:24
  • @stephen updated the model Commented Dec 21, 2015 at 5:14
  • You model is IEnumerable<string> (not bool) so just manually generate the html using <input type="checkbox" name="reports" value="sdf"> id you want to receive an array of the values which have been checked. Commented Dec 21, 2015 at 5:17
  • But ideally you should be creating a view model and binding to it. The model would contain (say) string Name and bool IsSelected and use a for loop to generate the controls - refer this answer for an example Commented Dec 21, 2015 at 5:19
  • @SivaRajini Can you tell us what you are trying to do? As far as I can see, you have a list of reports and you want the user to select a few of those reports and post back to your controller. So that you can generate reports for the selected report names? Commented Dec 21, 2015 at 5:23

2 Answers 2

2

First of all, you need a viewModel for the report;

public class Report
{
    public string ReportName { get; set; }
    public bool IsSelected { get; set; }
}

Then let's assume you are using your "Home" controller for this. Go to Home folder which is under Views in your project.

Create a new Folder called DisplayTemplates. This name and folder position is extremely important. So use the exact name.

Then create a new view inside DisplayTemplates. The name of your view has to be Report.cshtml.

In Report.cshtml view, use the below code;

model MvcApplication1.Models.Report

@Html.CheckBoxFor(m=>m.IsSelected)
@Html.Label(Model.ReportName, Model.ReportName)

Then go back in to Home view folder and select Index.cshtml. Add below code to index page.

@model List<MvcApplication1.Models.Report>

@using (Html.BeginForm("ShowReports", "Home", Model, FormMethod.Post))
{
     @Html.DisplayFor(m => m)

    <button type="submit">Show Selected Reports</button>
}

Now add below code to HomeController.

    public ActionResult Index()
    {
        List<Report> reports = new List<Report>()
        {
            new Report(){ReportName = "Report 1", IsSelected = false},
            new Report(){ReportName = "Report 2", IsSelected = false},
            new Report(){ReportName = "Report 3", IsSelected = false},
            new Report(){ReportName = "Report 4", IsSelected = false},
            new Report(){ReportName = "Report 5", IsSelected = false},
        };
        return View(reports);
    }

    public ActionResult ShowReports(IEnumerable<Report> model)
    {
        Debug.Write(model.Count());
        return View(); //create a blank view called ShowReport.
    }

Add a break point to ShowReport and have a look at what you are getting for the model variable.

Sign up to request clarification or add additional context in comments.

Comments

1

You can use the CheckBoxFor(x=>x) helper if you have that option available. I was creating a form which was self generating and I need didn't have a hardcoded model to build the form from.

So to fix this issue, I looked at the result and if the checkbox was being submitted checked it would return two values (true/false) like you stated. if the check box wasn't checked it would submit only false, the hidden value.

There was other ways people where suggesting using javascript, but what if javascript was disabled? So i decided to use this in the controller which receives a form collection.

public ActionResult methodName (FormCollection collecitonName)
        {
            foreach (var key in collecitonName.AllKeys)
            {
                var isCheckboxTrue = Request.Form[key].Contains("true")
            }
         }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.