I am developing a website with asp.net mvc 4,
I have a method that checks user whether registered or not. I use ajax for it, if user already registered, I would like to write a message on a div on registration webpage, if not, I would like to register user and return home page. I hope its clear. because it seems a bit difficult to understand.
script type="text/javascript">
$(function() {
$('#form0').submit(function(event) {
event.preventDefault();
var result = "";
var form = $(this);
$.ajax({
url: "CheckUser", //form.attr("action"),
data: form.serialize(),
beforeSend: function() {
$("#ajax-loader").show();
},
complete: function() {
$("#ajax-loader").hide();
},
error: function(request, status, error) {
var obj = jQuery.parseJSON(request.responseText);
if (request.status == 403) // authorize error
{
myRouting(obj.LogOnUrl, "403 error", "error");
} else {
alert("Error Occured: " + error.responseText + "\nError Code: " +
request.responseText + " -- " + request.responseText);
var msg = "Error: " + error + "\nErrorCode: " + request.status;
myRouting("", msg, "error");
}
},
success: function (data) {
$.each(data, function() {
result += this.Name + "\n";
});
myRouting("", data, "data");
}
});
return false;
});
$(function myRouting(site, message, type) {
if (type == "error") {
$('#searchFailed').remove();
$('<div id="searchFailed"></div>').appendTo('#searchMain');
$('#searchFailed').append("<p>" + message + "</p>");
}
});
});
</script>
@using (Html.BeginForm("CheckUser", "Account", FormMethod.Post, new { id = "form0" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary()
<fieldset>
<legend>Registration Form</legend>
<ol>
<li>
@Html.LabelFor(m => m.UserName)
@Html.TextBoxFor(m => m.UserName)
</li>
<li>
@Html.LabelFor(m => m.Name)
@Html.TextBoxFor(m => m.Name)
</li>
<li>
@Html.LabelFor(m =>m.Password)
@Html.PasswordFor(m => m.Password)
</li>
<li>
@Html.Label("Confirm Password")
@Html.PasswordFor(m => m.Password)
</li>
<li>
@Html.LabelFor(m => m.Email)
@Html.TextBoxFor(m => m.Email)
</li>
</ol>
<input type="submit" value="Register" />
</fieldset>
}
And here is my accountcontroller
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Register(User model)
{
if (ModelState.IsValid)
{
// Attempt to register the user
try
{
var result = helper.RegisterUser(model);
if (result)
return RedirectToAction("Index", "Home"); //Json(Url.Action("Index", "Home"));
}
catch (MembershipCreateUserException e)
{
ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
}
}
return View(model);
}
public ActionResult CheckUser(User model)
{
bool result = helper.IsUserExist(model);
if (result)
{
ModelState.AddModelError("", ErrorCodeToString(MembershipCreateStatus.DuplicateUserName));
return Content("User is already created !!!!");
}
return Register(model);
}
I know that in ajax, its not possible to redirect action, but if i could do it "error" in ajax, i can redirect it in ajax..
return content is always being "success" in ajax, even user is not registered before, it stils being "success"