简体   繁体   中英

How can i Pass values of checkBox those are checked to controller action in asp.net mvc 3 without using Ajax/JQuery in asp.net Mvc3

    1. I have done it with using Ajax post method .But my problem is that with Ajax we can not download a file so i needed a different idea.Following is my code

        Asp.net MVC Ajax Post: <script type="text/javascript"> $(document).ready(function () { $('#btn').click(function () { var vals = []; $('input:checkbox[name=Blanks]:checked').each(function () { vals.push($(this).val()); }); $.ajax({ // Check the value. type: 'POST', enctype: 'multipart/form-data', url: '/Home/Extract', // data: { 'name': checkboxData }, data: { 'name': JSON.stringify(vals) }, // contentType: 'application/json; charset=utf-8', // No need to define 

      contentType success: function (result) {

        }, error: function (err, result) { alert("Error in delete" + err.responseText); } }); }); return false; }); </script> @foreach (var m in (List<string>)ViewData["list"]) { <ul> <li> <input type="checkbox" class="myCheckbox" name="Blanks" value="@m"/> <img src="@m" alt=""/> </li> } ASp.net MVC controller Section: [AcceptVerbs(HttpVerbs.Post)] public ActionResult Extract(string[] name) { return view(); } 

      I neede without Ajax post method .Is it possible?

You have a bunch of checkboxes with the same name and different values. You could post them to a method that takes a FormCollection, ie.

    public ActionResult Test(FormCollection collection)
    {
        string results = collection["Blanks"];
    }

This would give you a comma-delimited list of values (or null, where no checkboxes are ticked).

Alternatively, if you have the possible values as an array on the server then you could give the checkboxes names according to their array values, which would mean you could do something like this:

@using (Html.BeginForm("Test","Home"))
{
    @Html.CheckBox("Blanks[0]", false);
    @Html.CheckBox("Blanks[1]", false);
    @Html.CheckBox("Blanks[2]", false);

    <input type="submit" value="Submit" />
}

giving you an array of booleans in your Test method:

public ActionResult Test(bool[] Blanks)
{        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM