connectseekers.com

Thursday, 2 November 2017

Access CheckBox and CheckBoxFor Value From View To Controller In Asp.Net MVC, C#.Net

11/02/2017 - By Pranav Singh 0

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.

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

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 uncheck and click on submit.


Now check the debugger.



About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com
Email Id : contact@aspdotnet-pools.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Top jQuery Plugins

Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top