Ajax.BeginForm to specify "GET" type posting

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)

Upvotes: 3

Views: 8207

Answers (2)

Josh
Josh

Reputation: 7405

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.

Upvotes: 3

Darin Dimitrov
Darin Dimitrov

Reputation: 1038730

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.

Upvotes: 7

Related Questions