Ajax.BeginForm to specify “GET” type posting

这一生的挚爱 提交于 2019-12-10 19:20:12

问题


My view is as below:

<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" />
}  

Whenever I try to compile and I view the source of the html page that I get, I see,

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

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

Any ideas why ? Thanks in advance.

EDIT:

I even checked my page source by view-source in my browser. This shows:

<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" />

(Notice that the script (jquery-unobstrusive) actually is there)


回答1:


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.




回答2:


Even though including the jQuery file works, I still feel it's incorrect to have POST in there. You can "override" it by specifying it in the html attributes:

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

You may be able to set it so null or blank, too, using this hack. Just FYI.



来源:https://stackoverflow.com/questions/19248043/ajax-beginform-to-specify-get-type-posting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!