Click here to Skip to main content
15,903,030 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi everyone...

how to get checkbox selected value in mvc controller.
please help
Posted
Comments
Jameel VM 19-Aug-13 1:54am    
please post your code
Apali 19-Aug-13 2:01am    
see in my view page there are more than 10 checkboxes....when i will click on checkbox i want to get that checkbox value in my controller...for that i used

TempData["msg"]=objSalesReport.hiddenvalue;

here objSalesReport object but after clicking on more than one checkbox I am getting only current checkbox value....how I will get all the checkbox value whatever i clicked...if you know please reply soon...

1 solution

Suppose your mark up goes like below..

XML
<input type="checkbox" value="1000"  name="HTML" id="1"  class="box">HTML<br /><br />
<input type="checkbox"  name="C" value="1500" id="2"  class="box">C#<br /><br />
<input type="checkbox"  name="JQuery" value="2000" id="5"  class="box">JQuery<br /><br />
<input type="button" id="GetTotal" value="Get total amount"><br />


make an ajax call to server as below sending your data to action method of controller

JavaScript
$(document).ready(function () {
    $('#GetTotal').on('click', function () {
        var prices = [];
        $('input:checked').each(function () {

            prices.push($(this).attr("value"));
        });
        $.ajax({
            url: "/Home/ArrayHandler",
            type: "POST",
            data: {coursePrices:prices},
            dataType: "json",
            traditional:true,
            success: function () {
                alert("ajax request to server succeed");
            }
        });
    });
});


Your controller method will go like below

C#
public ActionResult ArrayHandler(IEnumerable<int> coursePrices)
{
    return View();
}


Hope this will help you...
 
Share this answer
 
Comments
Apali 19-Aug-13 2:37am    
no this one i don't want in view page...in controller values should come whatever i will click on checkboxes..
Jitendra Sabat 19-Aug-13 2:48am    
Above code does the same as you say.Whatever checkbox checked by you will be sent to controller.Others will. remain intact as usual.
Member 12066406 2-Jan-16 13:35pm    
This works for me. Thank you!
touinta 1-Nov-16 2:31am    
Hello can you help me a bit? What the coursePrices stand for? Do I have to declare it in my view?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900