Reputation: 137
I'm new in asp.net mvc
and write this razor code for show me check box component:
@Html.CheckBox("FlatFile",false)
I want write razor c# script to check if that check box is checked then work some thing, I don't want to write javascript or anything, I just want write c# razor script code for that purpose.
How can I write that?
Upvotes: 1
Views: 12664
Reputation: 290
Your checkbox code:
@Html.CheckBox("FlatFile",false)
Now you can use the CheckBox name (i.e., FlatFile) to do stuff in your controller like :
public ActionResult Something(IEnumerable<bool> FlatFile)
{
if(FlatFile!= null) --you can give your condition here
{
--do something
}
else
{
--do something else
}
}
A sample example:-
This is my controller code:-
public ActionResult test()
{
return View();
}
[HttpPost]
public ActionResult test(bool FlatFile)
{
if(FlatFile==true)
{
ViewBag.Message = "Selected";
return View();
}
else (FlatFile == false)
{
ViewBag.Message="Not selected";
return View();
}
}
This is my view code(i.e., test.cshtml):-
@using (Html.BeginForm("test", "User", FormMethod.Post,
new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<fieldset>
<legend>Test CheckBox</legend>
<table>
<tr>
<td>@Html.Label("Select"):</td>
<td>@Html.CheckBox("FlatFile", false)</td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Check" /></td>
</tr>
<tr>
<td>@ViewBag.Message</td>
</tr>
</table>
</fieldset>
}
Upvotes: 1
Reputation: 1222
You can check this using FormCollection in your controller action. try using below code:
[HttpPost]
public ActionResult Index(FormCollection frm)
{
bool MyBoolValue = Convert.ToBoolean(frm["FlatFile"].Split(',')[0]);
return View();
}
Upvotes: 2