0
我有jquery自動完成功能工作。即我輸入一個字段,然後根據字段中鍵入的字符串返回數據。我的問題是,我也想在確定我應該返回當前字段時使用另一個字段中的數據。例如,我只想返回屬於某個公司的驅動程序。該數據在另一個字段中輸入。如何修改下面的代碼來執行此任務?假設我有一個「Model.Company」屬性。ASP.NET MVC自動完成 - 依賴多個輸入字段
視圖(它的一部分):
<%= Html.AutoCompleteTextBox2("DrvId", "DriverID", Model.DrvId, new { style = "width:200px;" })%>
<%= Html.InitializeAutoComplete2("DrvId", "DriverID", "Driver ID", Model.DrCmpId, Url.Action("Drivers", "AutoComplete"), new { delay = 100, minChars = 1 }, true)%>
控制器:
public ActionResult Drivers(string q)
{
List<TmwXref> driverList = baseService.GetTypeList("Driver");
for (var i = 0; i < driverList.Count; i++)
{
if (driverList[i].Value.StartsWith(q, StringComparison.OrdinalIgnoreCase))
{
Response.Output.Write("{0}|{1}\r\n", driverList[i].Value + " - " + driverList[i].Description, i);
}
}
return new CancelViewResult();
}
UI助手:
public static string InitializeAutoComplete2<T>(this HtmlHelper<T> html,
string textBoxName, string fieldName, string labelName, string fieldDesc,
string url, object options, bool wrapInReady)
{
StringBuilder sb = new StringBuilder();
if (wrapInReady) sb.AppendLineFormat("<script language='javascript'>");
if (wrapInReady) sb.AppendLineFormat("$().ready(function() {{");
sb.AppendLine();
sb.AppendLineFormat(" $('#{0}').autocomplete('{1}', {{", textBoxName.Replace(".", "\\\\."), url);
PropertyInfo[] properties = options.GetType().GetProperties();
for (int i = 0; i < properties.Length; i++)
{
sb.AppendLineFormat(" {0} : {1}{2}",
properties[i].Name,
properties[i].GetValue(options, null),
i != properties.Length - 1 ? "," : "");
}
sb.AppendLineFormat(" }});");
sb.AppendLine();
sb.AppendLineFormat(" $('#{0}').result(function(e, d, f) {{", textBoxName.Replace(".", "\\\\."));
sb.AppendLineFormat(" $('#{0}').val(d[1]);", fieldName);
sb.AppendLineFormat(" }});");
sb.AppendLine();
if (wrapInReady) sb.AppendLineFormat("}});");
if (wrapInReady) sb.AppendLineFormat("</script>");
return sb.ToString();
}
public static string AutoCompleteTextBox2<T>(this HtmlHelper<T> html, string textBoxName, string fieldName, string value, object htmlAttributes)
{
return string.Format("{0} {1}", html.TextBox(textBoxName, value, htmlAttributes), html.Hidden(fieldName));
}