Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
public PartialViewResult AddBranchContact(BranchViewModel objBranchContact)
{
    List<BranchContact> BranchContactList = new List<BranchContact>();
    if (ModelState.IsValid)
    {
       if (Session["BranchContactList"] == null)
       {
           objBranchContact.BranchContactDetails.BranchContactID = 1;
           BranchContactList.Add(objBranchContact.BranchContactDetails);
           Session["BranchContactList"] = BranchContactList;
    return PartialView("PartialViewAddBranchContact", BranchContactList);
       }
       else
       {
    List<BranchContact> ContactList = Session["BranchContactList"] as List<BranchContact>;
           ContactList.Add(objBranchContact.BranchContactDetails);
           Session["BranchContactList"] = ContactList;
    return PartialView("PartialViewAddBranchContact", ContactList);
       }
    }
    else
    {
       return PartialView("PartialViewAddBranchContact");
    }

View :
HTML
@using (Ajax.BeginForm("AddBranchContact", new AjaxOptions { HttpMethod = "POST", OnSuccess = "$('#myModal1').modal('hide');", UpdateTargetId = "myTable" }))
{}

Here OnSuccess i am hiding a modal popup. But i now have a scenario where in there are some fields which are mandatory to be filled in the popup.
If not the debugger doesnt go into ModelState.IsValid so basically OnSuccess should become false.

How do i notify this?
What i want is if Modelstate.isvalid is not satisfied it shouldnt hide the modalpopup
Posted
v2

1 solution

You need to define the js for OnBegin (string) available in AjaxOptions to validate the form and return false if the mandatory fields are not filled in. You can either use jQuery to verify if the mandatory fields are filled in or use the jQuery Validate[^] plugin to do the validation for you! There are numerous tutorials / links about OnBegin for you get familiar w/ it!!

Straight out of google in no particular order!:

jquery - how to validate the request using onBegin in Ajax.Begin Form? - Stack Overflow[^]
How to use onBegin method with Ajax.BeginForm | Yasser Shaikh[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900