2015-10-15 66 views
1

我正在做我的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員工(INT ID),我需要通過( '#服務')值。 可以做嗎?誰能幫幫我嗎?

+0

當然下面的變化,但你的方法只接受一個參數。你想傳遞的2個參數是什麼? –

+0

在('#'Location).change()函數中,我需要傳遞(「#Location」)和(「#Service」)值 – Nisha

+0

請參閱答案,但未顯示方法的簽名應該是什麼。另外,不要將「SelectList」返回給客戶端 - 您只需返回您從未使用過的額外數據。相反,請返回一組匿名對象。 –

回答

1

,只要你想使用的jQuery.getJSON

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

感謝您的幫助,我現在更改了代碼。它工作:) – Nisha

1

data參數是的,你只需要一個參數添加到下面的ControllerAction您可以通過儘可能多的價值。

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