如何在MVC中通过$ .getJson传递2个参数?

Nis*_*sha 1 asp.net-mvc-4

我在MVC中做我的应用程序。我确实在list中层叠了dropdownlist。从下拉列表中选择值时,我通过$ .getJson()将值传递给了控制器。我正在将一个ID传递给控制器​​。我的问题是我需要将2个ID传递给控制器​​。是否可以向控制器发送2个ID?

我的查看页面是

 <div>
               @Html.DropDownList("Business", ViewBag.CategoryID as SelectList ,"Select the Business Category", new { id = "Business" })
           </div>
           <div>
               <label>Select the Service Type</label>
               @*<select id="Buname" name="buname" style="width:150px"></select>*@
               @Html.DropDownList("Service",  ViewBag.Service as SelectList,"Select", new { id = "Service", name ="buname"})
           </div>
           <div>
               <label> Select the Location</label>
               <select id="Location" name="location" style="width:150px"></select>
           </div>
           <div>
               <label> Select the Location</label>
               <select id="Employee" name="employee" style="width:150px"></select>
           </div> 

<script type="text/javascript">
      $(function () {

          $('#Business').change(function () {

              $.getJSON('/Appt/Categories/' + $('#Business').val(), function (data) {

                  var items = '<option> Any Hospital</option>'
                  $.each(data, function (i, Service) {
                      debugger;
                      items += "<option value='" + Service.Value + "'>" + Service.Text +
                          "</option>";
                  });

                  $("#Service").html(items);

              });


          });

          $('#Service').change(function () {

              $.getJSON('/Appt/Location/' + $('#Service').val(), function (data) {

                  var items = '<option> Any Location </option>';
                  $.each(data, function (i, location) {

                      items += "<option value='" + location.Value + "'>" + location.Text +
                          "</option>";
                  });

                  $("#Location").html(items);

              });

          });

          $('#Location').change(function () {

              $.getJSON('/Appt/Employee/' + $('#Location').val(),  function (data) {
                  var items = '<option> Any Employee </option>';
                  $.each(data, function (i, employee) {

                      items += "<option value='" + employee.Value + "'>" + employee.Text +
                          "</option>";
                  });

                  $("#Employee").html(items);
                  $("Service").html(items);


              });
          });

      });


    </script>
Run Code Online (Sandbox Code Playgroud)

我的控制器代码是

public ActionResult WaitingList()
        {
            SYTEntities ca = new SYTEntities();
            ViewBag.CategoryId = new SelectList(ca.BusinessCategories.ToList(), "CategoryId", "CategoryName");
            ViewBag.Service = new SelectList(ca.tblBusinessCategories.ToList(), "BusinessID", "BusinessName");
            return View();
        }

 public JsonResult Categories(int id)
        {
            SYTEntities db = new SYTEntities();
            var business = from s in db.tblBusinessCategories
                           where s.CategoryId == id
                           select s;
            return Json(new SelectList(business.ToArray(),"BusinessID", "BusinessName"), JsonRequestBehavior.AllowGet);
        }
  public JsonResult Location(int id)
        {


            SYTEntities db = new SYTEntities();
            var location = from s in db.tblBusinessLocations
                           where s.BusinessID == id

                           join loc in db.tblLocations
                            on s.LocID equals loc.LocationId
                           select loc;



            return Json(new SelectList(location.ToArray(), "LocationId", "LocationName"), JsonRequestBehavior.AllowGet);

        }
        public JsonResult Employee(int id)
        {

            SYTEntities db = new SYTEntities();
            var emp = from s in db.Employees
                      where s.LocationId == id && s.BusinessID == 91
                      select s;
            return Json(new SelectList(emp.ToArray(), "EmpId", "EmpName"), JsonRequestBehavior.AllowGet);
        }
Run Code Online (Sandbox Code Playgroud)

在公共JsonResult Employee(int id)中,我需要传递('#Service')值。有可能吗?谁能帮帮我吗?

小智 5

您可以使用jQuery.getJSONdata参数传递任意多个值。

var url = '@Url.Action("Categories", "Appt")'; // don't hard code your urls!
$.getJSON(url, { id: $('#Business').val(), otherProperty: anotherValue }, function (data) {
Run Code Online (Sandbox Code Playgroud)