开发者

ASP.NET MVC AutoComplete - Rely on more then one input field

I have the jquery autocomplete functionality working. i.e. I type in a field and then return data based on the string typed in the field. My problem is that I would also like to also use data keyed in another field when dete开发者_开发技巧rmining what I should be returning the current field. For example I only want to return Drivers that belong to a certain company. This data is inputted in another field. How do I modify the code below to perform this task? Assume I have a "Model.Company" property.

View (part of it):

<%= 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)%>

Controller:

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 Helper:

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));
    }


Is the second data entry field going to be autocomplete also or a drop down list with list of companies for example?

I'm not sure what your TmwXref object looks like but I'm sure you tie your drivers to companies some how.

If it's just a drop down list to select a company to filter on, you could probably just do something similar to:

public ActionResult Drivers(string q)
{
    var company = GetSelectedCompanyHere();

    List<TmwXref> driverList = baseService.GetTypeList("Driver");

    for (var i = 0; i < driverList.Count; i++)
    {
        if (driverList[i].Company == company && driverList[i].Value.StartsWith(q, StringComparison.OrdinalIgnoreCase))
        {
            Response.Output.Write("{0}|{1}\r\n", driverList[i].Value + " - " + driverList[i].Description, i);
        }
    }

    return new CancelViewResult();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜