jquery - ASP .NET MVC issue with Ajax.BeginForm replaces all view content with JSON data -


i have created sample asp.net mvc website in vs 2015 , in view using extension ajax.beginform post login credentials controller, , on onsuccess callback want check server errors , if show error user else - redirect home page, here code (it returns error intentionally test callback):

model

[required] public string username { get; set; } [required] public string password { get; set; } 

view

<script>     function onloginsuccess(result) {         /*           instead of getting here after returning result            controller view(in browser) replaced with:           {"status":"error","message":"invalid email or password.","returnurl":"/"}         */         if (result.status === "ok") {             window.location.href = result.returnurl;         } else {             alert(result.message);         }     }      $("#buttonlogin").on('click', function (e) {         $("#formlogin").submit();     }); </script> <section id="loginform"> @using (ajax.beginform("login", "custom", null, new ajaxoptions { httpmethod = "post", onsuccess = "onloginsuccess" }, new { @class = "form-horizontal", role = "form", id = "formlogin" })) {     @html.antiforgerytoken()     <h4>login:</h4>     <hr />     <div class="form-group">         <label id="loginvalidation" class="col-md-12 validation-label"></label>     </div>     <div class="form-group">         <label class="col-md-12 control-label" for="loginusername">username</label>         <div class="col-md-12">             <input class="form-control" id="loginusername" name="username" type="text" value="" />         </div>     </div>     <div class="form-group">         <label class="col-md-12 control-label" for="loginpassword">password</label>         <div class="col-md-12">             <input class="form-control" id="loginusername" name="password" type="password" value="" />         </div>     </div>     <div class="form-group">         <div class="col-md-offset-4 col-md-12">             <input type="button" id="buttonlogin" value="log in" class="btn btn-default" />         </div>     </div> } </section> 

controller

[httppost] [validateantiforgerytoken] public jsonresult login(loginmodel credentials) {     string returnurl = "/";     string status = asyncrequeststatus.resulterror; //"error"     string message = "invalid email or password.";      return json(new { status = status, message = message, returnurl = returnurl }); } 

if displaying json returned login() method, because have not included jquery.unobtrusive-ajax.js file in view. result, ajax.beginform() falls making normal submit


Comments