开发者

Why is my asp.net mvc form POSTing instead of GETting?

My code is straightforward enough:

    <% using(Html.BeginForm(FormMethod.Get)) %>
    <% { %>
        Search for in Screen Name and Email: <%: Html.TextBox("keyword", Request.QueryString["keyword"]) %>
        <button type=submit>Search</button>
    <% } %>

The issue I'm running into is that when I submit this form, the values are not added to the querystring. Instead, it appears that the form is submitting by a post request. When I look at the generated HTML, I have this:

    <form action="/find/AdminMember/MemberList" method="post">
        Search for in Screen Name and Email: <input id="keyword" name="keyword" type="text" value="" />
        <button type=submit>Search</button>
    </form>

Does anyone know why? This seem开发者_运维知识库s pretty simple and straighforward to me.


The correct signature of the BeginForm helper is this:

<% using(Html.BeginForm("SomeAction", "SomeController", FormMethod.Get)) %>
<% { %>
    Search for in Screen Name and Email: 
    <%: Html.TextBox("keyword", Request.QueryString["keyword"]) %>
    <button type="submit">Search</button>
<% } %>

When you write BeginForm(FormMethod.Get) you are basically invoking this signature where the routeValues parameter has nothing to do with FormMethod.Get and which uses POST as default verb.


You're passing FormMethod.Get as the routeValues parameter

You will have to qualify your action and controller to set the FormMethod of the form tag

using(Html.BeginForm("action", "controller", FormMethod.Get))

FormExtensions.BeginForm Method


Looks as if you are not using the correct overload for BeginForm, check here for the various overloads.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜