Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC3 jQuery Autocomplete - User must select option, ID of option sent to server
    primarykey
    data
    text
    <p><strong>Background</strong> <br /> Currently in a project of mine I am using jQuery <code>autocomplete</code> for a few fields.</p> <p>To provide context, the application records <code>Runs</code>. Each <code>Run</code> must have a <code>Route</code> associated with it. A <code>Route</code> meaning, where the user ran.</p> <p>When the user types in a <code>Route</code>, a list of their <code>Routes</code> gets displayed by the <code>autocomplete</code> options, but the database requires the <code>RouteID</code> for validation.</p> <p>To compensate for this, I stored the <code>RouteID</code> in a <code>HiddenFor</code> HtmlHelper. When the user selects the route from the <code>autocomplete</code>, the <code>HiddenFor</code> gets assigned.</p> <p><strong>What my problem is</strong> <br /> If the user types in the full name of the <code>Route</code>, instead of selecting it from the <code>autocomplete</code> list or enters a <code>Route</code> that does not exist, the <code>HiddenFor</code> will not get assigned. When this happens, I have to find the <code>Route</code> by its name and validate that it exists on the server.</p> <p>I would like to not have to create this work-around for every <code>autocomplete</code>. </p> <p><strong>The bottom line</strong> <br /> Is there anyway to make the <code>autocomplete</code> act more like a <code>select list</code>? I want the user to have no choice but to select the text of one option from the <code>autocomplete</code> list, and have the value of the selected option is sent to the server.</p> <p>If I have to stick to the <code>HiddenFor</code> method, is there at least a way to force the user to select an option from the <code>autocomplete</code> list?</p> <hr> <p><em>Below is the code I am currently using</em></p> <p><strong>Mark-up</strong></p> <pre><code>@Html.LabelFor(model =&gt; model.RouteID, "Route") &lt;input type="text" data-autocomplete-url="@Url.Action("../Route/GetRoutesByUser")" /&gt; @Html.HiddenFor(m =&gt; m.RouteID) </code></pre> <hr> <p><strong>jQuery</strong></p> <pre><code> $('*[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); } }); }); </code></pre> <hr> <p><strong>Code</strong></p> <pre><code>public ActionResult GetRoutesByUser(string term) { var routeList = db.Routes.Where(r =&gt; r.Name.Contains(term)) .Take(5) .Select(r =&gt; new { id = r.RouteID, label = r.Name, name = "RouteID"}); return Json(routeList, JsonRequestBehavior.AllowGet); } </code></pre>
    singulars
    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. 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