【发布时间】:2014-10-20 08:56:07
【问题描述】:
我遇到的问题类似于这两个 StackOverflow 帖子 here 和 here。但是,我的情况完全不同,以至于我无法进行所需的更正。
就像那两个帖子一样,当浏览器执行帖子控制器操作时,我似乎正在失去上下文。
场景如下:
- 页面加载
- 用户点击添加按钮。
- 模态对话框加载部分视图,其中包含添加新对象所需的字段。
- 表单已完成,用户点击保存。
- 添加完成的控制器操作(添加记录就好了)然后重定向到步骤 1 的控制器操作。想法是这将关闭模式并将用户返回到步骤 1 中的页面。
问题当然是重定向导致 $ (jquery) is undefined 错误。
步骤 1 控制器操作:
public ActionResult Configure()
{
....code...
return View("~/FolderPath.../Configure.cshtml", model);
}
第 2 步 jQuery 启动模态:
$("#new").click(function (e)
{
e.preventDefault();
var window = $("#window").kendoWindow(
{
content: {
url: "@Html.Raw(Url.Action("CreateEditRecord", "Controller"))",
data: { .... }
}
});
window.data('kendoWindow').center();
});
第 3 步和第 4 步模态提交:
$("#save").click(function (e)
{
e.preventDefault();
...some validation stuff here.....
$("#formCreateEditRecord").submit();
});
第 5 步(这些是我尝试过的不同方法,它们都失去了 jQuery 上下文)
public ActionResult CreateEditRecord(NewRecordModel model)
{
...add new record to db etc.....
//return RedirectToAction("Configure");
//return Configure();
//return PartialView("~/FolderPath/CreateEditRecord.cshtml", model);
//tried this last one to return to modal window just trying to figure things out.
}
编辑
@SSA 和@Ryios。你的解释很清楚,确实解决了问题,但如果你能再忍受我几秒钟......
我了解部分视图没有完整页面的全部权重(脚本标签等)。我仍然不清楚的是为什么它甚至会回到局部视图?我在第 5 步中的后控制器操作重定向到配置操作,这是一个完整视图,所以我希望页面加载完整视图。
在更改和重新加载整个页面之前,MVC 视图引擎是否仍然呈现部分视图,即使只是几毫秒?
【问题讨论】:
标签: c# jquery asp.net-mvc asp.net-mvc-4