I have a piece of code in jQuery/.NET MVC that looks like this:
$('form').submit(function (event) {
event.preventDefault();
event.stopPropagation(); // only neccessary if something above is listening to the (default-)event too
if ($(this).valid()) {
$('#confirmLoader').show();
$('.btnregister').hide();
var formdata = new FormData($(this).get(0));
$.ajax({
url: this.action,
type: this.method,
data: formdata,
processData: false,
contentType: false,
success: function (result) {
if (result.statusCode == 700) {
// var loginWrapper = result.View.find(".loginWrapper").innerHTML;
// var sas = $($.parseHTML(result.View)).find(".loginWrapper").prevObject[45].innerHTML;
// $($.parseHTML(result.View)).find(".loginbody").html()
//
//console.log($($.parseHTML(result.View)).find(".loginbody").html());
$(".loginbody").html($($.parseHTML(result.View)).find(".loginbody").html());
//$("body").html(result.View);
//
// console.log(result.View);
return false;
}
else if (result.statusCode == 200) {
window.location.href = result.url;
}
}
});
}
return false;
});
So the basic purpose of this method is to post the data and log in the user onto the system.
In the case of the when the server responds with error code 700 => I render the entire page in .net MVC action and return it as a result to the user and should be displayed;
So for example, if I entered xy mail and wrong password, the message is displayed the first time, but the second time I get the entire page rendered, without even getting the javascript code to execute.
I have to note two things:
$("body").html(result.View);
Like this, the response always works when the status code is 700 and it always displays the message properly.
However, I don't want to replace the entire DOM of the user, only the portion where the login form is, and display the error message.
Thats why I tried this:
$(".loginbody").html($($.parseHTML(result.View)).find(".loginbody").html());
With this solution it only works first time... Second time it renders me the entire page, and I can see the page being refreshed...
Why is this happening with solution #2?
P.S. this is the code that is returned in background:
ModelState.AddModelError("WrongUsernamePassword", "Wrong username or password!");
return Json(new { statusCode = 700, View = this.RenderView("Login", model) });
Why doesn't 2nd method works?
Can someone help me out?