Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Alright, after a lot of fiddling around I came up with the following implementation:</p> <p>The code below is a <code>HtmlHelper</code> called <code>@Html.AutocompleteWithHiddenFor</code>. The HtmlHelper will create an <code>input</code> HTML element with a <code>data-autocomplete-url</code> property based on the <code>controller</code> and <code>action</code> passed in. </p> <p>If the <code>input</code> element needs a <code>value</code> then you can pass that in as well. A <code>HiddenFor</code> will be created for the <code>Model</code> property passed in and a <code>ValidationMessageFor</code> will be created for the <code>Model</code> as well.</p> <p>Now all I have to do is use <code>@Html.AutocompleteWithHiddenFor</code>, and pass in whatever expression I need with the controller and action (and possibly the value) <strong>to get the autocomplete functionality and have the ID pass to the server instead of the text</strong>.</p> <p><strong>jQuery</strong></p> <pre><code>$(function () { function log(id, name) { var hidden = $('#' + name); hidden.attr("value", id); } $('*[data-autocomplete-url]') .each(function () { $(this).autocomplete({ source: $(this).data("autocomplete-url"), minLength: 2, select: function (event, ui) { log(ui.item.id, ui.item.name); }, change: function (event, ui) { if (!ui.item) { this.value = ''; } else { log(ui.item.id, ui.item.name); } } }); }); }); </code></pre> <hr> <p><strong>AutocompleteHelper class</strong></p> <pre><code>public static class AutocompleteHelper { /// &lt;summary&gt; /// Given a Model's property, a controller, and a method that belongs to that controller, /// this function will create an input html element with a data-autocomplete-url property /// with the method the autocomplete will need to call the method. A HiddenFor will be /// created for the Model's property passed in, so the HiddenFor will be validated /// and the html input will not. /// &lt;/summary&gt; /// &lt;returns&gt;&lt;/returns&gt; public static MvcHtmlString AutocompleteWithHiddenFor&lt;TModel, TProperty&gt;(this HtmlHelper&lt;TModel&gt; html, Expression&lt;Func&lt;TModel, TProperty&gt;&gt; expression, string controllerName, string actionName, object value = null) { // Create the URL of the Autocomplete function string autocompleteUrl = UrlHelper.GenerateUrl(null, actionName, controllerName, null, html.RouteCollection, html.ViewContext.RequestContext, includeImplicitMvcValues: true); // Create the input[type='text'] html element, that does // not need to be aware of the model String textbox = "&lt;input type='text' data-autocomplete-url='" + autocompleteUrl + "'"; // However, it might need to be have a value already populated if (value != null) { textbox += "value='" + value.ToString() + "'"; } // close out the tag textbox += " /&gt;"; // A validation message that will fire depending on any // attributes placed on the property MvcHtmlString valid = html.ValidationMessageFor(expression); // The HiddenFor that will bind to the ID needed rather than // the text received from the Autocomplete MvcHtmlString hidden = html.HiddenFor(expression); string both = textbox + " " + hidden + " " + valid; return MvcHtmlString.Create(both); } } </code></pre> <hr> <p><strong>View</strong></p> <pre><code>@Html.LabelFor(model =&gt; model.RouteID, "Route") @Html.AutocompleteWithHiddenFor(model =&gt; model.RouteID, "Route", "GetRoutesByUser") </code></pre> <p>Or if it needs a <em>value</em></p> <pre><code>@Html.LabelFor(model =&gt; model.Route, "Route") @Html.AutocompleteWithHiddenFor(model =&gt; model.RouteID, "Route", "GetRoutesByUser", @Model.RouteName) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload