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

三世轮回 提交于 2019-12-24 07:18:47

问题


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?


回答1:


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');



来源:https://stackoverflow.com/questions/41950483/mvc-trigger-submit-event-of-partial-view-in-main-view-using-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!