1

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>

2 Answers 2

2

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

1
  • Was looking for something else.. but this is interesting :) Never knew you could to that :)
    – Piotr Kula
    Jan 9, 2014 at 10:54
0

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

See here for more details

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.