【发布时间】:2013-09-04 19:52:19
【问题描述】:
仍在尝试解决这个问题,这是我的工作流程:
- 一旦点击提交,jquery会发送一个post请求来调用该方法
- 方法返回部分视图
- 显示在
<div id = "messageForm">...</div>部分
下面是表单视图:
//SignUp.cshtml:
<div id ="messageForm">
@using (Ajax.BeginForm("SignUp", "MVP", new AjaxOptions
{
Confirm = "Are you sure you want to send this message?",
HttpMethod = "Post",
InsertionMode = InsertionMode.Replace,
LoadingElementId = "loading",
UpdateTargetId = "messageForm"
})) {
@Html.AntiForgeryToken();
@Html.ValidationSummary(true)
<fieldset>
<legend>
messageModel
</legend>
<p>
<input type ="submit" value ="Send Message" />
</p>
</fieldset>
这里是控制器:
//MVPController
[HttpPost]
public ActionResult SignUp(MVCView model){
return PartialView("_ThankYou");
}
public ActionResult SignUp(){
return View();
}
这是视图文件夹中的局部视图:
ThankYou.cshtml:
<h1>Thank you so much! We will contact you later</h1>
在测试时,我没有看到确认对话框,它重定向到感谢页面
谁能告诉我为什么会这样?
【问题讨论】:
-
Controller方法叫SignUp,而Html.AjaxForm中的action是“ThankYou”,你确定有效吗?
-
假设您有错误,我添加了一个答案?问题中没有说明这一点?
-
您的标记中是否有任何有趣的事情(例如嵌套表单),因为听起来表单不是异步提交的?
-
您是否在布局/视图中引用了
jquery.unobtrusive-ajax.js文件?因为这个文件是Ajax.BeginForm工作所必需的... -
@LifeScript 您需要在布局或视图中包含
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>的脚本。此文件不包含在默认的 mvc 项目模板中,您需要手动添加它,它与您的 web.config 无关。
标签: asp.net-mvc asp.net-mvc-3 asp.net-mvc-4