我有一个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