Jquery 如何获取DropDownList的值并将其传递到MVC5中的AJAX调用中?
我有一个MVC5应用程序,我有一个模型,定义如下:Jquery 如何获取DropDownList的值并将其传递到MVC5中的AJAX调用中?,jquery,ajax,asp.net-mvc,json,jquery-ui-autocomplete,Jquery,Ajax,Asp.net Mvc,Json,Jquery Ui Autocomplete,我有一个MVC5应用程序,我有一个模型,定义如下: public class Request { ... [ForeignKey("State")] public int StateID { get; set; } public virtual State State { get; set; } public string ServiceName { get; set; } } <div class="form-group"&
public class Request
{
...
[ForeignKey("State")]
public int StateID { get; set; }
public virtual State State { get; set; }
public string ServiceName { 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>
我的状态模型定义如下:
public class State
{
public int StateID { get; set; }
public string StateCode { get; set; }
public string StateName { get; set; }
}
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);
}
在我看来,我正在工作,我有这样的想法:
public class Request
{
...
[ForeignKey("State")]
public int StateID { get; set; }
public virtual State State { get; set; }
public string ServiceName { 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>
因此,一旦用户在我的视图中选择了StateID,我如何将其值发送到AJAX调用和GetBusinessDescription方法,以便仅过滤处于所选状态的业务?例如,在源选项中使用AJAX并传递额外的参数。这里StateId是state dropdownlist的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);
}
});
看一看,这很好,唯一需要做的就是将数据类型从jsonp更改为json。感谢您的回答。很高兴看到使用Jquery UI自动完成传递除所需“术语”之外的更多参数的实现。:)