I use modal bootstrap to display the login form and the registration form on the site for users
And by using ModelState.AddModelError, I show the error message, but when the user enters the wrong password, the message is not shown in the same Modal, and it enters another page and shows the message.
How can I receive the error message in the same Modal?
View :
@model DataLayer.LoginViewModel
@{
Layout = null;
}
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<br />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Username, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Username, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Username, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Password, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Password, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group" style="width:130%;">
@Html.LabelFor(model => model.RememberMe, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
@Html.EditorFor(model => model.RememberMe)
@Html.ValidationMessageFor(model => model.RememberMe, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="ورود به سایت" class="btn btn-primary" />
</div>
</div>
</div>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
Controller :
public ActionResult Login(LoginViewModel login, string ReturnUrl="/")
{
if(ModelState.IsValid)
{
if(loginRepository.IsExistUser(login.Username,login.Password))
{
FormsAuthentication.SetAuthCookie(login.Username, login.RememberMe );
if (Session["ShopCart"] != null)
{
ShopCartViewModel newCart = new ShopCartViewModel();
List<ShopCartModel> cart = Session["ShopCart"] as List<ShopCartModel>;
var shopcarts = shopCartRepository.GetAllShopCart().Where(c => c.UserName == login.Username);
foreach (var item in cart)
{
if (shopcarts.Any(c => c.HandmadeID == item.HandmadeID))
{
foreach(var c in shopcarts)
{
if (c.HandmadeID == item.HandmadeID)
{
c.Count += 1;
shopCartRepository.Update(c);
}
}
}
else
{
var handmade = db.handMades.Find(item.HandmadeID);
int Price = 0;
string handmadeName = "";
string ImageName = "";
Price = int.Parse(handmade.Price);
ImageName = handmade.ImageName;
handmadeName = handmade.Title;
newCart.ImageName = ImageName;
newCart.Count = 1;
newCart.Price = Price;
newCart.Sum = Price;
newCart.UserName = login.Username;
newCart.Title = handmadeName;
newCart.HandmadeID = handmade.HandMadeID;
newCart.UserID = loginRepository.GetIdByName(login.Username).ToString();
newCart.Mobile = 0;
newCart.Address = null;
shopCartRepository.InsertShopCart(newCart);
}
}
}
Session["ShopCart"] = null;
shopCartRepository.Save();
return Redirect(ReturnUrl);
}
else
{
ModelState.AddModelError("UserName", "کاربری یافت نشد");
}
}
return View(login);
}