【问题标题】:POST method with AJAXOption in MVC3 call GET requestMVC3 中带有 AJAXOption 的 POST 方法调用 GET 请求
【发布时间】:2012-01-10 14:39:29
【问题描述】:

我对 MVC3 中的 AjaxOption 感到困惑,我想发出 post 请求,但它总是发出 get 请求并重定向到新的 url。

我的 .cshtml 代码如下

<div >  

     @Html.ActionLink("Check Availability", "ValidateUsername", "Wizard",  new { username = "arun"},
                            new AjaxOptions()
                                {
                                    Url="/Wizard/ValidateUserName",
                                    UpdateTargetId = "msg",
                                    HttpMethod = "POST",
                                    LoadingElementId = "progress"

                                })
    </div>
    <div id="progress">
        <img alt="" src="../../Content/Images/progress.gif" width="20px" height="20px" style="display: none" />
    </div>
    <div id="msg">
    </div> 

和控制器操作如下

public ActionResult ValidateUsername(string username)
        {
            Thread.Sleep(200);
            //return Json(!username.ToLower().Equals("arun"));
            return Json(true , JsonRequestBehavior.AllowGet);
        }

总是发出 get 请求而不是 POST 请求并重定向到新 URL http://localhost:55152/Wizard/ValidateUsername?username=arun,为什么?

和下面生成的HTML代码

<div >

            <a Confirm="" HttpMethod="POST" InsertionMode="Replace" LoadingElementDuration="0" LoadingElementId="progress" OnBegin="" OnComplete="" OnFailure="" OnSuccess="" UpdateTargetId="msg" Url="/Wizard/ValidateUserName" href="/Wizard/ValidateUsername?username=arun">Check Availability</a>
        </div>
        <div id="progress">
            <img alt="" src="../../Content/Images/progress.gif" width="20px" height="20px" style="display: none" />
        </div>

        <div id="msg">
        </div>

【问题讨论】:

    标签: asp.net-mvc-3 razor


    【解决方案1】:

    你需要使用@Ajax.ActionLink而不是@Html.ActionLink通过ajax发布数据

    【讨论】:

    • 正在寻找其他东西.. 但这很有趣 :) 从来不知道你能做到这一点 :)
    【解决方案2】:

    HTML 中的锚标记总是执行获取请求。您应该使用 Jquery 帖子。

    详情请见here

    【讨论】:

      猜你喜欢
      • 2012-08-29
      • 2014-01-29
      • 1970-01-01
      • 2017-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-27
      • 1970-01-01
      相关资源
      最近更新 更多