MVC drop-down box Interaction Effect (single choice), mvc Interaction

Source: Internet
Author: User

MVC drop-down box Interaction Effect (single choice), mvc Interaction

The drop-down box shows the linkage effect. We take the Department-position as an example. When selecting a department, we will not talk much about the job associated with the Department. For details, refer to the previous article.

View:

Dept is the department attribute, deptlist is the department drop-down box attribute, job is the position attribute, and joblist is the position drop-down box attribute. For details about binding the drop-down box, refer to the previous section.

@using (Html.BeginForm("aaai003sch", "aaa", FormMethod.Post, new { @class = "form-horizontal", role = "form" })){    @Html.AntiForgeryToken()    <div class="modal-body">        <div class="form-horizontal">               <div class="form-group">                    @Html.LabelFor(m => m.dept, new { @class = "col-sm-2 control-label" })                        <div class="col-sm-10">                            @Html.DropDownListFor(model => model.dept, Model.deptlist, new { @class = "form-control select2 ", style = "width: 100%;" })                            @Html.ValidationMessageFor(m => m.dept, "", new { @class = "text-danger" })                       </div>               </div>               <div class="form-group">                @   Html.LabelFor(m => m.job, new { @class = "col-sm-2 control-label" })                       <div class="col-sm-10">                            @Html.DropDownListFor(model => model.job, Model.joblist, new { @class = "form-control select2 page-select2-area", style = "width: 100%;" })                            @Html.ValidationMessageFor(m => m.job, "", new { @class = "text-danger" })                       </div>               </div>        </div>    </div></div>

When the department changes, the position also changes accordingly:

// Get the hotel $ ("# dept") based on the city "). change (function () {var url = rootUrl + "aaa/GetJobByDept"; var dept = $ (this ). val (); // obtain the department Value var job =$ ("# job"); job. empty (); // clear the value of the current position // This sentence is very important, because we use the select2 plug-in. If this plug-in is not used, we can remove this job. select2 ('val ', ''); $. ajax ({cache: false, type: "GET", url: url, data: {"Dept": dept}, success: function (data) {$. each (data, function (id, option) {job. append ($ ('<option> </option> '{.val(option.id}.html (option. name) ;}); job. trigger ('change');}, error: function (xhr, ajaxOptions, thrownError) {toastr ["error"] ("select a Department ");}});});

Run the URL in js and write this program in the controller:

[Description ("obtain position by Department")] [AcceptVerbs (HttpVerbs. get)] [LoginAllowView] public ActionResult GetJobByDept (string dept) {if (String. isNullOrEmpty (dept) {throw new ArgumentNullException ("dept");} StringBuilder sb = new StringBuilder (); sb. append ("SELECT jobid, jobname"); sb. append ("FROM job_file"); sb. append ("left join dept_file ON jobdept = deptid"); sb. appendFormat ("WHERE deptid = '{0}'", dept); DataTable dt = sqlHelper. getData (sb. toString (); var result = dt. asEnumerable (). select (row => new Item {Name = Utils. objToStr (row ["jobname"]), Id = Utils. objToInt (row ["jobid"], 0 )}). toList (); return Json (result, JsonRequestBehavior. allowGet );}

 

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.