3

我的看法如下:

<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>

@using (Ajax.BeginForm(new AjaxOptions { HttpMethod = "Get", InsertionMode = InsertionMode.Replace, UpdateTargetId = "DisplayPatients" }))
{
    <input type="search" name="searchTerm" />
    <input type="submit" value="Do Search" />
}  

每当我尝试编译并查看我得到的 html 页面的源代码时,我看到,

<form action="/" data-ajax="true" data-ajax-method="Get" data-ajax-mode="replace" data-ajax-update="#DisplayPatients" id="form0" 
   method="post">    

但是,在我的 Ajax.BeginForm 中,我指定了HttpMethod = Get. 尽管如此,我还是method = "post"在输出 html 页面中得到了。

任何想法为什么?提前致谢。

编辑:

view-source我什至在浏览器中检查了我的页面源。由此可见:

<script src="/Scripts/jquery.unobtrusive-ajax.js" type="text/javascript"></script>
<script src="/Scripts/jquery.validate.js" type="text/javascript"></script>
<script src="/Scripts/jquery.validate.unobtrusive.js" type="text/javascript"></script>

<form action="/" data-ajax="true" data-ajax-method="Get" data-ajax-mode="replace" data-ajax-update="#DisplayPatients" id="form0" method="post">    <input type="search" name="searchTerm" />

(请注意,脚本(jquery-unobtrusive)实际上是存在的)

4

2 回答 2

7

But, in my Ajax.BeginForm, I specify the HttpMethod = Get. Inspite being this, I get the method = "post" in the output html page.

The jquery.unobtrusive-ajax.js script ignores the method attribute and uses data-ajax-method (if present). So the actual request will be GET. Look at the Network tab of your Google Chrome developer console to see.

于 2013-10-08T12:49:18.587 回答
3

即使包含 jQuery 文件有效,我仍然觉得在其中放置 POST 是不正确的。您可以通过在 html 属性中指定它来“覆盖”它:

@using (Ajax.BeginForm("Action", "Controller", null, new AjaxOptions { HttpMethod = "GET", InsertionMode = InsertionMode.Replace, UpdateTargetId = "results" }, new { @id = "search", @role = "search", @method="get" }))

您也可以使用此 hack 将其设置为 null 或空白。仅供参考。

于 2014-04-21T19:45:22.427 回答