MVC selects the corresponding check box based on the value of the checkbox and gets the value of the selected value

Source: Internet
Author: User


650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M02/08/2D/wKiom1ndOBjwnpd4AAAMkennUYc858.png-wh_500x0-wm_ 3-wmp_4-s_1559099237.png "title=" qq picture 20171010213055.png "alt=" Wkiom1ndobjwnpd4aaamkennuyc858.png-wh_50 "/>

View Code

@using (Html.BeginForm ("H", "DEFAULT1"))//DEFAULT1 Controller H is a method

{

Loop add 5 x Checkboxs

for (int i = 0; i < 5; i++)
{
<input type= "checkbox" id= "@i" name= "Checko" value= "@i"/><span>@i</span>

}
<input type= "Submit" id= "BTN" value= "Submit"/>
}



Controller code

Int[] Array types can be customized

Checko gets the value of the checked checkbox in the form of an array

Public ActionResult H (int[] Checko)
{

Define a ArrayList array for easy debugging look

ArrayList list = new ArrayList ();

foreach (var item in Checko)
{

You can write your own INSERT statement (add data)

List. ADD (item. ToString ());//This is easy to debug to see
}
Return Content ("");
}


Simulation Database

Public ActionResult F ()
{

The value of the checkbox in the View interface is a loop-added 0-4

Here you define an array to simulate the query out of the data using JQ's Ajax call today and tick based on value values

ArrayList list = new ArrayList ();
List. ADD ("1");
List. ADD ("3");
List. ADD ("0");
return Json (list);
}



 $.ajax ({
                 Type: "Post",
                 URL: "/default1/f",//Controller name/method name
                 async:false,//Synchronous Asynchronous
                 success:function (data) {
                     var json = data;
                     $.each (JSON, function (I, t) {

var s = json[i];//are 1, 3, 0, respectively.
$ ("input[value=" +s+ "]"). attr ("Checked", "checked");//Check by Value
})
}
})

This article is from the "go to Daniel's direction" blog, make sure to keep this source http://949770845.blog.51cto.com/10170051/1971295

MVC selects the corresponding check box based on the value of the checkbox and gets the value of the selected value

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.