This
article will show you how you can access checkbox and checkboxfor value from
view to controller in asp.net mvc, c#.net. In this I have used HtmlHelper.
Some of my previous article are as follows: Group CheckBox In C# WinForm, Bind CheckBox List From DataBase And Validate One Item Selection Using EntityFrameWork In Asp.Net MVC, How to Bind CheckBoxlist By Model Data and Get Selected Value In Controller in asp.net MVC.
Some of my previous article are as follows: Group CheckBox In C# WinForm, Bind CheckBox List From DataBase And Validate One Item Selection Using EntityFrameWork In Asp.Net MVC, How to Bind CheckBoxlist By Model Data and Get Selected Value In Controller in asp.net MVC.
So for this article first we will create a new asp.net mvc application and a model class file. In this class file we will add the below mention code.
CheckBoxFor:
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Web;
namespace
MvcApplication12.Models
{
public class CheckBoxModel
{
public bool Status { get; set; }
}
}
|
Now we will add the below controller code.
using
MvcApplication12.Models;
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace
MvcApplication12.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
[HttpGet]
public ActionResult
Index()
{
CheckBoxModel
_checkboxmodel = new CheckBoxModel();
return
View(_checkboxmodel);
}
[HttpPost]
public ActionResult
Index(CheckBoxModel _checkboxmodel)
{
bool value =
_checkboxmodel.Status;
return View();
}
}
}
|
In above code I have passed the model class as a return value. Now create the view and add the below code.
@model MvcApplication12.Models.CheckBoxModel
@{
ViewBag.Title
= "Access CheckBox and CheckBoxFor
Value From View To Controller In Asp.Net MVC, C#.Net";
}
@using (Html.BeginForm("Index", "Home"))
{
<b>Example Of
CheckBoxFor </b><br />
@Html.CheckBoxFor(m
=> m.Status, false)<span>CheckBox Value</span><br />
<input type="submit" value="Submit" />
}
|
In above code just check the highlighted part of the code. In this I have bind the checkbxfor with model. So whatever value we will pass in model property checkbox will checked unchecked as per the model value.
Now select and check
the output.
Click on submit and your will get .
Now unselect and click on submit button
Click on submit and your will get .
Now in debugger check the output.
CheckBox:
For
checkbox add a controller class file and add the below code in it.
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace
MvcApplication12.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
[HttpGet]
public ActionResult
Index()
{
return View();
}
[HttpPost]
public ActionResult
Index(bool
SelectedCheckBoxId)
{
return View();
}
}
}
|
Now create the view and add the below code into it.
@using (Html.BeginForm("Index", "Home"))
{
<b>Example Of
CheckBox </b><br />
@Html.CheckBox("SelectedCheckBoxId", false)<span>CheckBox Value</span><br />
<input type="submit" value="Submit" />
}
|
In above code I have just passed the name of control same as the parameter value in post method of index in controller.
Now we have done run the application to check the output. Check the checkbox and click on submit.
Now click on submit to check the output.
Now check the debugger.
Now click on submit to check the output.
Now uncheck and click on submit.
Now check the debugger.
0 comments:
Please let me know your view