我有以下索引视图
@model VirtualCampus2.Models.EmployeeViewModel
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Index</h2>
@using(Ajax.BeginForm("GetEmployee", "Employee",
new AjaxOptions
{
HttpMethod = "get",
InsertionMode = InsertionMode.Replace,
OnSuccess = "updateDivList",
UpdateTargetId = "divList" }))
{
@Html.DropDownListFor(m=>m.id, Model.DDLCollecton)
<input type="text" name="SearchString" />
<input type="submit" value="search" />
}
<div id="divList">
@Html.Partial("_EmployeeList", Model.Employees);
</div>
<script type="text/javascript">
function updateDivList() {
$('#divList').html();
}
</script>
控制器:
public class EmployeeController : Controller
{
//
// GET: /Employee/
testdbEntities db = new testdbEntities();
public ActionResult Index()
{
List<SelectListItem> ddlColl = new List<SelectListItem>
{
new SelectListItem{ Text="By Emp No", Value="1", Selected=true},
new SelectListItem{ Text="By Name", Value="2"}
};
var employees = db.Employees.Select(e => new EmployeeModel
{
EmpID=e.EmpId,
FirstName=e.FirstName,
LastName=e.LastName,
DeptId=e.DepartmentId,
DepartmentName=e.Department.Name
});
EmployeeViewModel evm = new EmployeeViewModel { DDLCollecton = ddlColl, Employees = employees };
return View(evm);
}
public ActionResult GetEmployee(string id, string SearchString)
{
IEnumerable<EmployeeModel> results=null;
if (id == "1")
{
var searchID = ( SearchString == "" ) ? 0 : int.Parse(SearchString);
results = db.Employees.Where( e => ( SearchString == "" || e.EmpId == searchID ) )
.Select(e => new EmployeeModel
{
EmpID = e.EmpId,
FirstName = e.FirstName,
LastName = e.LastName,
DeptId = e.DepartmentId,
DepartmentName = e.Department.Name
}).ToList();
}
else
{
}
return PartialView("_EmployeeList", results);
}
当我请求ajax时,它会将请求作为非ajax调用发送,但会将数据作为常规http请求获取,如this video
所示为什么会发生这种情况?如何解决这个问题?