提问人:ahmed abdelaziz 提问时间:8/24/2023 更新时间:8/24/2023 访问量:29
更改 employeeId 自动完成时无法显示 employeeName?
I can't display employeeName when change employeeId auto complete?
问:
我正在从事 asp.net mvc 自动完成项目。我遇到了无法显示的问题Employee Name
更改自动完成时输入文本 IDLineManagerName
employeeId
员工 ID 表示
@Html.EditorFor(model => model.LineManager, new { htmlAttributes = new { @class = "form-control", id = "txtLineManagerId" } })
员工姓名代表
<input type="text" id="LineManagerName" class="form-control" />
从自动完成列表中选择员工 ID 时,员工姓名未显示在基于更改的 ID 的输入文本上LineManagerName
txtLineManagerId
我尝试的是
public ActionResult GetAllEmployeeBasedSearchText(string searchText)
{
JDEUtility jde = new JDEUtility();
List<object> employeeListCriteria = new List<object>();
employeeListCriteria = jde.GetAllEmployeeBasedSearchText(searchText);
return Json(employeeListCriteria, JsonRequestBehavior.AllowGet);
}
public static List<object> GetAllEmployeeBasedSearchText(string searchText)
{
OleDbConnection con = new OleDbConnection(connectionString);
string query = "";
query = "SELECT cast(EMP.YAAN8 as varchar(20)) as EmployeeID,EMP.YAALPH AS EmployeeName FROM CRPDTA.F060116 EMP WHERE cast(EMP.YAAN8 as varchar(20)) LIKE '%" + searchText + "%' WITH UR";
List<object> ApplicationsDataValues = new List<object>();
try
{
using (var command = con.CreateCommand())
{
command.CommandText = query;
command.CommandType = CommandType.Text;
command.Connection.Open();
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
ApplicationsDataValues.Add(new
{
EmployeeID = reader.GetFieldValue<string>(0)
});
}
reader.Close();
}
}
con.Close();
return ApplicationsDataValues;
}
catch (Exception e)
{
return new List<object>();
}
}
$(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 };
}))
}
});
}
});
});
答:
1赞
M. Radinmehr
8/24/2023
#1
@html。EditorFor 是 Razor,在服务器端生成,不能在客户端通过 js 更改。
作为解决方案,通过 javascript 或 jquery,您可以将请求发送到服务器并调用返回 IActionResult 的方法,并为其创建部分视图(包含类似 @html 的 razor。EditorFor),并在该请求的返回值处,将现有 Razor 替换为新 Razor。
下面是现有 Razor 的示例代码
:
<span id="razor-to-replace">
@Html.EditorFor(model =>
model.LineManager, new { htmlAttributes
= new { @class = "form-control", id =
"txtLineManagerId" } })
</span>
通过ajax发送请求:
JQuery.post('/home/GetAllEmployeeBasedSearchText',{searchText:searchText},function(value){
$('#razor-to-replace').html(value);
});
服务器上的方法(返回部分视图):
Public IActionResult GetAllEmployeeBasedSearchText(string searchText)
{
//do process and return model.LineManager
//send result to view in what ever way you like
ViewBag.LineManager = LineManager;
return View();
}
部分视图(替换为现有视图):
@{Layout=null;}
@Html.EditorFor(model =>
ViewBag.LineManager as List<int>, new { htmlAttributes
= new { @class = "form-control", id =
"txtLineManagerId" } })
上一个:表单错位
评论