我有一個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方法,以便過濾器處於選中狀態只選擇商家?
看一看這個http://stackoverflow.com/questions/11852282/jquery-ui-autocomplete-with-json-and-ajax – Mairaj 2014-09-11 11:27:00