mardi 28 mars 2017

Resource not found error in MVC when posting using beginform

The below code is not working properly as expected.

The controller have an action method

        public ActionResult LoginResult()
            string name = Request.Form["name"];             
            return View("Dashboard");
     public ActionResult Dashboard()
        string strName = Request["username"].ToString();
        return View();

and in view i have a form

 @using (Html.BeginForm("LoginResult", "Dashboard", FormMethod.Post))
     @Html.EditorFor(model =>model.username)
    <button type="button" id="ajax_method">submit Via AJAX</button>


and im submitting this using a function

    $(function () {
        $('#ajax_method').click(function (e) {
                type: "POST",
                url: "Dashboard/LoginResult",                             //Your Action name in the DropDownListConstroller.cs
                data: "{'AJAXParameter1':'" + $('#username').val() + "'}",  //Parameter in this function, Is case sensitive and also type must be string
                contentType: "application/json; charset=utf-8",
                dataType: "json"

            }).done(function (data) {
                //Successfully pass to server and get response
                if (data.result = "OK") {
                    alert("submit successfully.");
            }).fail(function (response) {
                if (response.status != 0) {
                    alert(response.status + " " + response.statusText);


but im getting 404 error while clicking the button.

Even I tried adding that routerspecifications in route config.

            name: "login",
           url: "{controller}/{action}/{id}",
           defaults: new { controller = "Dashboard", action = "LoginResult", id = UrlParameter.Optional }

and modified the cshtml again. But this is also not working.

 @using (Html.BeginForm("login", "Dashboard", FormMethod.Post))
     @Html.EditorFor(model =>model.username)
    <button type="button" id="ajax_method">submit Via AJAX</button>


It may be a silly error but it spoil my day. Can anyone extend a hand to solve this issue.

Aucun commentaire:

Enregistrer un commentaire