jqueryajaxasp.net-mvcajax.beginform

MVC - Trigger submit event of partial view in main view using JQuery


I am using Ajax.BeginForm in my partial view to submit the data.

Partial view - _getCategoryMaster.cs

<div>
    @using (Ajax.BeginForm("Submit", "CategoryMasterDataEntry", new AjaxOptions { OnSuccess = "OnDatatSuccess", OnFailure = "OnDataFailure", HttpMethod = "POST" }, new { enctype = "multipart/form-data" }))
    {
           <div style="float:left;width:100%;">
                <hr class="horizontal-line-bottom" />
                <div class="form-buttons tab-footer">
                    <button class="btn btn-md" type="submit" id="saveData" @if (TempData["DisplayAccess"].ToString() == "view") { <text> disabled </text>  }>SAVE</button>
                </div>
            </div>
     }
</div>

Now i'm trying to call this submit event from my parent view on the click event of the link from Jquery

Parent View -

<html>
<head>
<script type="text/javascript">
 $(document).on('click', '.getProducts', function (e) {
        alert("Start");

        $('form#ajaxForm').trigger('submit');

        alert("End");
</script>
</head>
<body>
 <a id="anchId" href="javascript:void(0);" class="getProducts">
 <img src="~/Images/bullets.png" class="bullet-image" /> 
  <span class="menu-text">LinkName</span> 
  </a>
</body>
</html>

But its not firing that submit event of the partial view. I am getting those alerts (start and End). But its not firing that submit event.

What am I doing wrong here?


Solution

  • You do not have a form with id="ajaxForm". Replace new { enctype = "multipart/form-data" } (which is pointless since you cannot submit files using Ajax.BeginForm()) with new { id= "ajaxForm" }

    @using (Ajax.BeginForm("Submit", "...", new AjaxOptions { ... }, new { id = "ajaxForm" }))
    

    or simply use $('form').trigger('submit');