Arun Rana
Arun Rana

Reputation: 8606

POST method with AJAXOption in MVC3 call GET request

I am confused with AjaxOption in MVC3 , i would like to make post request but it's always make get request and redirects to new url.

my .cshtml code as per below

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

and controller action as per below

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

it's always make get request instead of POST request and redirect to new URL http://localhost:55152/Wizard/ValidateUsername?username=arun , why?

and HTML code generated as per below

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

Upvotes: 1

Views: 1130

Answers (2)

Anand
Anand

Reputation: 14915

Anchor tags in HTML always does a get request. You should either use Jquery post.

See here for more details

Upvotes: 0

shankhan
shankhan

Reputation: 6571

You need to use @Ajax.ActionLink instead of @Html.ActionLink to post data by ajax

Upvotes: 2

Related Questions