0

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.

Saranga
  • 3,178
  • 1
  • 18
  • 26

3 Answers3

0
CkbQuestion1 must be set as value of name attribute for checkbox. It will look like this in your Html.


<input type="checkbox" name=""/>

it will return Empty string if you didn't define the value of value attribute. If you don't define value attribute then it will post the NULL to server.

Anirudha Gupta
  • 9,073
  • 9
  • 54
  • 79
0

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" />

}
Razack
  • 950
  • 3
  • 13
  • 26
0

if you have property for that field than simply change your mark up and write

@Html.EditorFor(td => td.PropName)

and now you can access your checkbox value in controller.