When a user begins typing in the DRMCompanyName
input text box, and autocomplete feature fires that displays both the company name and the company id. When the use clicks on a selection, the company name and id are to be placed into the DRMCompanyName
text box and the id in the DRMCompanyId
text box just below.
When the json
results are returned from the controller, the code in the autocomplete ajax success
function populates the drop down list by setting the label
to be equal to the value
(company name) plus the key
(company id) being returned. Likewise the value
is set to just the key
(company id).
When the user selects a particular item, the label
is supposed to go in the DRMCompanyName
text box and the value
in the DRMCompanyId
. However, what winds up happening is the value
gets placed in both.
I've scoured my code over and over and cannot find out why the label
does not get placed in the DRMCompanyName
field.
jQuery
$(function () {
$('#DRMCompanyName').autocomplete({
source: function (request, response) {
$.ajax({
url: '@Url.Action("compSearchByName", "AgentTransmission")',
type: 'GET',
dataType: 'json',
data: request,
success: function (data) {
response($.map(data, function (value, key) {
return {
label: value + " " + key,
value: key
};
}));
},
});
},
minLength: 2,
select: function (event, ui) {
console.log(ui);
$('#DRMCompanyName').val(ui.item.label);
$('#DRMCompanyName').text(ui.item.label);
if ($('#DRMCompanyId').text() == '') {
$('#DRMCompanyId').val(ui.item.value);
$('#DRMCompanyId').text(ui.item.value);
}
}
});
});
Here is a sample screen shot of the ui
item from the select
function above (the company name is blacked out for privacy). When I click on this particular item in the autocomplete drop down, 200014
gets placed in both the DRMCompanyName
and DRMCompanyId
fields.
Razor Markup
<div class="M-editor-field">
@Html.TextBoxFor(model => model.DRMCompanyName)
@Html.ValidationMessageFor(model => model.DRMCompanyName)
</div>
<div class="M-editor-label">
@Html.LabelFor(model => model.DRMCompanyId)
</div>
<div class="M-editor-field">
@Html.TextBoxFor(model => model.DRMCompanyId, new { maxlength = 10, title = "Start typing company name to activate DRM Company Name lookup. When DRM Company is found, select to fill in DRM Company ID and DRM Company Name fields." })
@Html.ValidationMessageFor(model => model.DRMCompanyId)
</div>
EDIT
After following the suggestion in the answer below, I modified the select
function like so:
select: function (event, ui) {
console.log(tempResults[ui.item.value]);
$('#DRMCompanyName').val(tempResults[ui.item.value]);
$('#DRMCompanyName').text(tempResults[ui.item.value]);
if ($('#DRMCompanyId').text() == '') {
$('#DRMCompanyId').val(ui.item.value);
$('#DRMCompanyId').text(ui.item.value);
}
}
Based on the console.log
readout, this accesses the correct value when the user clicks on the autocomplete item. However, it still places the value in both text boxes. What I can't understand, when I select Inspect Element
, is that the correct value for DRMCompanyName
actually is placed in the HTML, however it does not appear on the screen, only the id
or value
(as opposed to label
).