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

时间:2015-10-15 05:55:46

标签: asp.net-mvc-4

我正在使用MVC进行应用程序。我在列表中做了级联下拉列表。从下拉列表中选择值时,我将$ .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>

我的控制器代码是

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);
        }

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

2 个答案:

答案 0 :(得分:5)

您可以使用jQuery.getJSONdata参数传递任意数量的值

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

答案 1 :(得分:1)

是的,您只需要向Controller Action添加一个参数,如下所示。

 public JsonResult Categories(int id, int id2)
        {
            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);
        }

以及您View

中的以下更改
$.getJSON('/Appt/Categories/' + $('#Business').val(), { id2: ('#otherId2').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);
    });