基于自动完成选择加载局部视图
Load Partial View based on Autocomplete Selection
我只是第一次学习 ASP.NET、MVC 和 JScript。我正在尝试使用自动完成中的选择来执行 ajax 调用获取数据并使用该数据加载部分视图。控制器获取带有选定 ID 的请求,但从不加载局部视图。我究竟做错了什么?
(视图和局部视图有不同的模型 - 我认为这不是问题)
控制器
public ActionResult GetEmployee(int id)
{
HumanResourcesManager man = new HumanResourcesManager();
var emp = man.GetEmployee(id);
return PartialView("_EmployeeDetails", emp);
}
VIEW
@model HumanResources.Web.Models.EmployeeModel
<p>
Selected Employee: @Html.TextBox("EmployeeSearch")
</p>
<script type="text/javascript">
$("#EmployeeSearch").autocomplete({
source: function (request, response) {
$.ajax({
url: "@(Url.Action("FindEmployee", "Employee"))",
type: "POST",
dataType: "json",
data: { term: request.term },
success: function (data) {
response($.map(data, function (item) {
return { label: item.DisplayName, value: item.DisplayName, id: item.Id };
}))
}
})
},
select: function (event, ui) {
if (ui.item) {
GetEmployeeDetails(ui.item.id);
}
}
});
function GetEmployeeDetails(id) {
$.ajax({
url: '/Employee/GetEmployee',
type: 'POST',
async: false,
data: { id: id },
success: function (result) {
$("#partialView").html(result);
}
});
}
</script>
<div id="#partialView">
@*Partial View Test*@
</div>
局部视图
@model HumanResources.Objects.Employee.EmployeeInformation
@{
Layout = null;
}
<h1>THIS IS A TEST</h1>
从您的局部视图 div ID 中删除 #
。只有在使用 jQuery 的 id selector.
时才添加它
<div id="partialView">
@*Partial View Test*@
</div>
我只是第一次学习 ASP.NET、MVC 和 JScript。我正在尝试使用自动完成中的选择来执行 ajax 调用获取数据并使用该数据加载部分视图。控制器获取带有选定 ID 的请求,但从不加载局部视图。我究竟做错了什么?
(视图和局部视图有不同的模型 - 我认为这不是问题)
控制器
public ActionResult GetEmployee(int id)
{
HumanResourcesManager man = new HumanResourcesManager();
var emp = man.GetEmployee(id);
return PartialView("_EmployeeDetails", emp);
}
VIEW
@model HumanResources.Web.Models.EmployeeModel
<p>
Selected Employee: @Html.TextBox("EmployeeSearch")
</p>
<script type="text/javascript">
$("#EmployeeSearch").autocomplete({
source: function (request, response) {
$.ajax({
url: "@(Url.Action("FindEmployee", "Employee"))",
type: "POST",
dataType: "json",
data: { term: request.term },
success: function (data) {
response($.map(data, function (item) {
return { label: item.DisplayName, value: item.DisplayName, id: item.Id };
}))
}
})
},
select: function (event, ui) {
if (ui.item) {
GetEmployeeDetails(ui.item.id);
}
}
});
function GetEmployeeDetails(id) {
$.ajax({
url: '/Employee/GetEmployee',
type: 'POST',
async: false,
data: { id: id },
success: function (result) {
$("#partialView").html(result);
}
});
}
</script>
<div id="#partialView">
@*Partial View Test*@
</div>
局部视图
@model HumanResources.Objects.Employee.EmployeeInformation
@{
Layout = null;
}
<h1>THIS IS A TEST</h1>
从您的局部视图 div ID 中删除 #
。只有在使用 jQuery 的 id selector.
<div id="partialView">
@*Partial View Test*@
</div>