Search code examples
asp.net-mvcasp.net-mvc-4asp.net-mvc-views

How to get value of Checkbox using formcollection in MVC4


I have a function, that collect the value of the controls of some view(.cshtml)

private string ExtractEmailId(FormCollection form)
{
    var value = form["CkbQuestion1"];

    return value;
}

I am receiving value of Checkbox as "true,false". I need the value of that control. How I can have that?

Any Idea please.


Solution

  • I expect you required the below code. Please replace your code with this.

    [HttpPost]
    public string ExtractEmailId(FormCollection form)
    {
         var value = form["CkbQuestion1"];
         return value;
    }
    

    And you view will be similar to

    @Using(Html.Beginform("ExtractEmailId"))
    {
         <input type="checkbox" name="CkbQuestion1" />
          <input type="submit" value="Submit" />
    
    }