我正在尝试让我的Ajax.BeginForm运行OnSuccess函数,但我不断收到firebug错误信息:
UpdateProjectDiv is not defined
return Function.constructor.apply(null, argNames);
以下是表单的内容:
<script type="text/javascript">
$(document).ready(function () {
function UpdateProjectDiv() {
var projid = $("#ddlProjectsManage").val();
$.post("/Manage/ProjectEmployeeList/", { projectid: projid }, function (data) {
populateDiv($("#divProjectsToFill"), data);
});
}
function populateDiv(div, data) {
div.html('');
div.append(data);
}
});
</script>
@using (Ajax.BeginForm("MoveToProject", new AjaxOptions { UpdateTargetId = "divAllEmployeesToFill", OnSuccess = "UpdateProjectDiv" }))
{
[insert non-relevant code here]
<input id="btnMoveEmpsToProject" type="submit" value=">>" />
@Html.Hidden("SelectedProjectID","9999999999999")
}
我的控制器动作:
[HttpPost]
public ActionResult MoveToProject(UnassignedEmployeeBindingModel model, int selectedprojectId)
{
var tempteam = _db.SpecTeams.SingleOrDefault(s => s.Name == "N/A" && s.ProjectId == selectedprojectId);
try
{
foreach (var employee in model.UnassignedEmployees)
{
var employeeObj = _db.Employees.SingleOrDefault(e => e.Id == employee.Employee.Id);
if (employee.IsSelected)
{
employeeObj.SpecTeamId = tempteam.Id;
_db.SaveChanges();
}
}
return RedirectToAction("UnassignedEmployeeList");
}
catch (Exception)
{
return RedirectToAction("Index");
}
}
没有OnSucess代码它可以正常工作,除了它不会自动刷新另一个部分(#divProjectsToFill)中的div。
此外,它似乎在jquery.unobtrusive-ajax.js文件中而不是在页面本身上查找函数UpdateProjectDiv。我想我可以在该文件中添加我的功能(虽然我还没有尝试过),但这听起来不是一个合适的解决方案。
谢谢!
答案 0 :(得分:21)
在$(document).ready(...)
事件处理程序之外中断函数定义:
<script type="text/javascript">
function UpdateProjectDiv() {
var projid = $("#ddlProjectsManage").val();
$.post("/Manage/ProjectEmployeeList/", { projectid: projid }, function (data) {
populateDiv($("#divProjectsToFill"), data);
});
}
function populateDiv(div, data) {
div.html('');
div.append(data);
}
$(document).ready(function () { /*... */});
</script>