【问题标题】:First day in AJAX in .NET MVC4, it doesn't work.NET MVC4 中 AJAX 的第一天,它不起作用
【发布时间】:2013-09-04 19:52:19
【问题描述】:

仍在尝试解决这个问题,这是我的工作流程:

  1. 一旦点击提交,jquery会发送一个post请求来调用该方法
  2. 方法返回部分视图
  3. 显示在<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 您需要在布局或视图中包含&lt;script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"&gt;&lt;/script&gt; 的脚本。此文件不包含在默认的 mvc 项目模板中,您需要手动添加它,它与您的 web.config 无关。

标签: asp.net-mvc asp.net-mvc-3 asp.net-mvc-4


【解决方案1】:

你有:

@using (Ajax.BeginForm("ThankYou", "MVP", new AjaxOptions

我认为应该是:

@using (Ajax.BeginForm("SignUp", "MVP", new AjaxOptions

第一个字符串是动作名称,控制器中只有SignUp

【讨论】:

  • 我改了,还是一样的问题
  • @LifeScript,MVCView 是什么?视图是强类型的吗,我在视图中看不到很多东西?另外,您是否缺少 ajax 的任何 JS 文件?
猜你喜欢
  • 1970-01-01
  • 2012-04-03
  • 1970-01-01
  • 2013-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-29
相关资源
最近更新 更多