I have the following pages -- >
Index page:
Create button Code:
@Ajax.ActionLink("Create New", "AjaxCreate", "User", new AjaxOptions()
{ HttpMethod = "POST", UpdateTargetId = "create-modal-body",
InsertionMode = InsertionMode.Replace,
OnBegin = "Loadingajax",
OnComplete = "showCreateModal"
}, new { @class = "btn btn-info",id="create-btn"})`
After I click on the create button i show a bootstrap modal like this
My form is in Partial View AjaxCreate
which works fine and gets loaded inside the create-modal-body
div (which is inside the modal body) when i click on 'Create new' Button
Partial View (AjaxCreate) Form Code:
@using (Ajax.BeginForm("Create", "User", new AjaxOptions() { UpdateTargetId = "create-modal-body", InsertionMode = InsertionMode.Replace }))
{
//Form Content here
}
Controller Code
public ActionResult AjaxCreate()
{
return PartialView();
}
[HttpPost]
public ActionResult Create(MAST_USER mast_user)
{
if (ModelState.IsValid)
{
mast_user.InsertTime = DateTime.Now;
db.MAST_USER.Add(mast_user);
db.SaveChanges();
return RedirectToAction("Index");
}
return PartialView("AjaxCreate", mast_user);
}
Note that return PartialView("AjaxCreate", mast_user);
gets executed if their is an error in validation
Now what I want is when I submit the form, if there is any validation errors, the errors must be shown right there in the modal itself
I searched and tried different combinations but when I submit an Invalid Form it goes to new page as shown below
I have included all the required validate js scripts in the index page and it works fine for 'create new' button but acts creepy while form submission.Form Submission while no validation errors works fine.