0

I have the following pages -- >

Index page:

enter image description here


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

enter image description here

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

List item

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.

Vishal Nair
  • 2,141
  • 3
  • 26
  • 39
  • Validate it on client side & use ajax post .. – Kaushik Thanki Sep 13 '15 at 19:45
  • sorry edited I have used POST only .. i think there must be some way instead of writing all client side validations manually when we have already specified it in the models ... – Vishal Nair Sep 13 '15 at 19:48
  • can u post ur demo @ https://dotnetfiddle.net/ ? – Kaushik Thanki Sep 13 '15 at 20:01
  • If you going to a new page, you have not included the relevant scripts (`jquery.unobtrusive-ajax.js`). But your POST method has `return RedirectToAction("Index");` which makes no sense since ajax stays on the same page (it does not redirect). –  Sep 13 '15 at 21:48
  • It returns RedirectToAction("Index") when the form is valid but if you see i am returning a partialview ` return PartialView("AjaxCreate", mast_user); ` in case form is invalid. Anyways I have included all the required js scripts in Index itself from where all the ajax calls are made . – Vishal Nair Sep 14 '15 at 01:46
  • @VishalNair, It does NOT redirect to `Index()` if you using `@Ajax.BeginForm()` correctly. (ajax calls do not redirect - they stay on the SAME page). The fact you are redirecting only confirms that you do not have `jquery.unobtrusive-ajax.js` loaded, or you have duplicate scripts or have placed your scripts in the wrong order because `Ajax.BeginForm()` is falling back to doing a normal submit (i.e. the equivalent of `Html.BeginForm()`) –  Sep 14 '15 at 03:59
  • 1
    There is a different approach, if am not wrong, to validate partial views and here is an **[answer](http://stackoverflow.com/a/9324173/2065039)** for that.. – Guruprasad J Rao Sep 14 '15 at 04:12
  • @GuruprasadRao Yes i got the solution .. wait i will elaborate more as an answer . – Vishal Nair Sep 14 '15 at 04:21

0 Answers0