2014-09-11 34 views
0

我有一個MVC5應用程序,我有這樣定義的模型:如何獲取DropDownList的值並將其傳遞給MVC5中的AJAX調用?

public class Request 
{ 
    ... 

    [ForeignKey("State")] 
    public int StateID { get; set; } 

    public virtual State State { get; set; } 

    public string ServiceName { get; set; } 
} 

而且我的狀態模型定義如下:

public class State 
{ 
    public int StateID { get; set; } 
    public string StateCode { get; set; } 
    public string StateName { get; set; } 
} 

而且在我看來,我是工作,我有這樣的事情:

 <div class="form-group"> 
      @Html.LabelFor(model => model.StateID, "State", htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.DropDownList("StateID", null, "Please select a state", htmlAttributes: new { @class = "form-control" }) 
       @Html.ValidationMessageFor(model => model.StateID, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.ServiceName, "Service", htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.ServiceName, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.ServiceName, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

而且一點是,我要插入自動完成成服務名稱我的輸入框,併爲它我已經寫了JsonResult甲基od定義如下:

public JsonResult GetBusinessDesriptions(int state, string term) 
    { 
     var results = db.Users.OfType<Business>().Where(b => b.StateID == state && (term == null || b.Description.ToLower().Contains(term.ToLower()))).Select(x => new { id = x.StateID, value = x.Description }).Take(5).ToList(); 

     return Json(results, JsonRequestBehavior.AllowGet); 
    } 

然後,我想在我的JS中使用AJAX調用它,但我不知道如何實現它。簡單地說,我想將用戶選擇的StateID傳遞給AJAX調用和調用GetBusinessDescription方法。

我有這樣的事情,但它不起作用,因爲我不知道如何通過在視圖中選擇的StateID,所以它只讀取選定狀態的業務。

$("#Service-Name").autocomplete({ 
    source: "/Home/GetBusinessDesriptions", 
    minLength: 2, 
    select: function (event, ui) { 
     $("#Service-Name").val(ui.item.value); 
     $("#Service-Name").text(ui.item.value); 
    } 
}); 

那麼,如何我可以送STATEID的價值,一旦用戶在我看來,以AJAX調用和我GetBusinessDescription方法,以便過濾器處於選中狀態只選擇商家?

+0

看一看這個http://stackoverflow.com/questions/11852282/jquery-ui-autocomplete-with-json-and-ajax – Mairaj 2014-09-11 11:27:00

回答

2

例如,在源選項中使用ajax並傳遞額外的參數。這裏StateId是狀態下拉列表的ID。

$("#Service-Name").autocomplete({ 
    source: function(request, response) { 
     $.ajax({ 
      url: "/Home/GetBusinessDesriptions", 
      dataType: "jsonp", 
      data: { 
      state:$("#StateID").val(), 
      term: request.term 
      }, 
      success: function(data) { 
      response(data); 
      } 
     }); 
     }, 
    minLength: 2, 
    select: function (event, ui) { 
     $("#Service-Name").val(ui.item.value); 
     $("#Service-Name").text(ui.item.value); 
    } 
}); 
+0

這工作得很好,唯一的事情是我需要改變具體的數據類型從jsonp到json。感謝你的回答。 – tett 2014-09-11 12:10:07

+0

很高興看到使用Jquery UI Autocomplete傳遞除了所需的「術語」之外的更多參數的實現。 :) – Irfan 2016-09-04 06:59:50

相關問題