I work on asp.net mvc . i face issue employee name display based on auto complete selection
and after select employee id from auto complete employee name display based on selection employee id
after click submit button create action fire and page load but employee name not display so
How to solve this issue please
full code details
JavaScript
@model HR.WorkforceRequisition.Models.ResignationRequester
@{
ViewBag.Title = "Create";
}
@using (Html.BeginForm("Create", "Resignation", FormMethod.Post, new { enctype = "multipart/form-data", @id = "mainform", style = "padding-top: 50px" }))
{
@Html.LabelFor(model => model.LineManager, htmlAttributes: new { @class = "control-label" })
*
@Html.EditorFor(model => model.LineManager, new { htmlAttributes = new { @class = "form-control", id = "txtLineManagerId" } })
@Html.Label("Line Manager Name", htmlAttributes: new { @class = "control-label" })
}
$(document).ready(function () {
$("#txtLineManagerId").autocomplete({
source: function (request, response) {
var searchText = $("#txtLineManagerId").val();
console.log("search text" + searchText)
$.ajax({
url: '@Url.Action("GetAllEmployeeBasedSearchText", "Resignation")',
data: { searchText: searchText },
method: "GET",
dataType: "json",
success: function (data) {
response($.map(data, function (item) {
console.log("data is" + item.EmployeeID);
return { label: item.EmployeeID, value: item.EmployeeID, employeeName: item.EmployeeName };
}))
}
});
},
position: { my: "right top", at: "right bottom" },
appendTo: '#searchContainer',
select: function (event, ui) {
$("#LineManagerName").val(ui.item.employeeName);
}
});C#
public class ResignationController : Controller
{
public ActionResult GetAllEmployeeBasedSearchText(string searchText)
{
JDEUtility jde = new JDEUtility();
List employeeListCriteria = new List();
employeeListCriteria = jde.GetAllEmployeeBasedSearchText(searchText);
return Json(employeeListCriteria, JsonRequestBehavior.AllowGet);
}
}
//post action result create
public async Task Create(ResignationRequester resignationRequester)
{
return View(resignationRequester);
}after create submit


Mohammad HussainRank #513 · 4.8K reputationPosted Aug 28, 2023, 3:19 AM EDT
Include a Hidden Field for Employee Name: Add a hidden input field in your form to store the selected employee name. This hidden field will hold the value of the selected employee name so that it can be posted back to the server during form submission.
Update the Autocomplete
selectCallback: Update theselectcallback of the autocomplete to set the value of the hidden input field (LineManagerNameHidden) along with the visible input field (LineManagerName).select: function (event, ui) { $("#LineManagerName").val(ui.item.employeeName); $("#LineManagerNameHidden").val(ui.item.employeeName); }Populate the Employee Name on Page Load: In your server-side code, when rendering the page after form submission, you need to populate the employee name from the model data into the visible input field (
LineManagerName) and the hidden input field (LineManagerNameHidden).