0

I am using Checkbox in my ASP.Net MVC project,

I can set check box by default as unchecked.

Age:
<input class="associationCheckbox" type="checkbox" name="age" />

When I post the form (For has only checkbox), the checkbox is not showing in formCollection (Here expected name and value are age, ‘false’).

Using below code,I was reading the velues.

 [HttpPost]
    public ActionResult Index(FormCollection formCollection)
    {
        foreach (var key in formCollection.Keys)
        {
            var value = formCollection[key.ToString()];
        }


        return RedirectToAction("CorticonIndex", "Test");
    }

If I check the check box at run time, then submit the form. I can get the name and value.

How to get the default values(false)If I don’t check checkbox?

I tried setting value property using model property but same issue is coming.

Model Class:

public bool CheckBoxvalue { get; set; }

this.CheckBoxvalue=false

Age: <input class="associationCheckbox" type="checkbox" name="age" value=@Model.CheckBoxvalue />

Can someone suggest me how to set and get default value.

I had look at below post from stack overflow but didn't info for my requirement.

How to set a CheckBox by default Checked in ASp.Net MVC

http://www.tutorialsteacher.com/mvc/htmlhelper-checkbox-checkboxfor

Thanks, Balu

Community
  • 1
  • 1
Balanjaneyulu K
  • 3,660
  • 5
  • 24
  • 45
  • You need to go to the MVC site and work through the tutorials to learn the basics. In MVC you bind to a model, using `HtmlHelper` method (to create a checkbox you use `@Html.CheckBoxFor(m => m.yourBooleanProperty) and you post back the model (do not use `FormCollection`) –  Jul 19 '16 at 11:24
  • Hi Stephen, Thank you for your suggestion. However, I don't want to sue @Html.CheckBoxFor(m => m.yourBooleanProperty). In my application,I have a form widget and on this widget I was placing this control. Once I click on submit,It will hit the ActionResult(FormCollection) of the respective form widget.I hope You understand. – Balanjaneyulu K Jul 19 '16 at 11:31
  • 1
    No. Can never understand why people want to write bad code. –  Jul 19 '16 at 11:33
  • Thanks. I will try to write good code. – Balanjaneyulu K Jul 19 '16 at 11:37
  • Hi, Can we set checkbox default value to false. – Balanjaneyulu K Jul 19 '16 at 11:57
  • Why on earth would you want to set its value to `false` - if you un-check the checkbox and submit, nothing is posted so the value is `false` (the defaukt value for `bool`) in the controller. If you check it and submit, a value of `false` is submitted, so the value is `false` in the controller. It would ALWAYS be `false` –  Jul 19 '16 at 12:00
  • http://stackoverflow.com/questions/11424037/does-input-type-checkbox-only-post-data-if-its-checked I have a field say “isPregnant”. I want to provide a check box and give control to user to check or uncheck. standard behavior is the value is only sent if the checkbox is checked. If user don’t check then the value is not posted upon clicking submit button. – Balanjaneyulu K Jul 19 '16 at 12:18
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/117706/discussion-between-stephen-muecke-and-balu). –  Jul 19 '16 at 12:19

1 Answers1

1

You can do something like this:

Age: <input class="associationCheckbox" type="checkbox" name="age" @(Model.CheckBoxvalue ? "checked" : "") value=@Model.CheckBoxvalue />

But it is always good to use HTMLHelper methods if you are using razor