0

This is follow up question on this (Bootstrap Modal Confirmation using ASP.Net MVC and a table) Using MVC .NetCore, I populate a "partial" view and a Table (called _Match_StatsViewAll.cshtml under a directory of the same name of my controller Match_Stats). It's being called from an Indexview:

public async Task<ViewResult> Match_StatsIndex(string message, string comp_id, string match_id, string team_id)//ACTION METHOD/View Results

Within that table (code below) , a list of records. Each row have a button to edit or delete. To delete a record, I need to have 4 IDs since the PK is made of those 4 keys. I was able to make it work with a javascript, however, I did not like the "confirm" display being produced. Thanks to this post, I was able to add a modal for each rows in the table and delete the record. However now, If I use the original json call to refresh the partial model (that only refresh the table), I have this screen: enter image description here

Here is a Row being populated from my Model:

<td>
    <div>
        @{i++; }
        <a onclick="showInPopup('@Url.Action("Match_StatsCreateOrEdit","Match_Stats",new {comp_id=item.Match_Stats.Comp_Id, team_id=item.Match_Stats.Team_Id,match_id=item.Match_Stats.Match_Id, match_Stat_Id=item.Match_Stats.Match_Stat_Id},Context.Request.Scheme)','Update A Stat')" class="btn btn-primary btn-xs"><i class="fas fa-pencil-alt"></i> Edit</a>
                       
        <form asp-action="Match_StatsDelete" asp-route-comp_Id="@item.Match_Stats.Comp_Id" asp-route-team_Id="@item.Match_Stats.Team_Id" asp-route-Match_Id="@item.Match_Stats.Match_Id"
                asp-route-Match_Stat_Id="@item.Match_Stats.Match_Stat_Id" onsubmit="return jQueryAjaxDelete(this)" class="d-inline">
            <button type="submit" class="btn btn-warning btn-xs"><i class="fas fa-trash"></i> Delete</button>
        </form>
                       

        @*//add the button to launch the modal*@
        <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#modal-removeplayers_@i"><i class="fas fa-trash"></i> Delete modal</button>

        <div class="modal fade" id="modal-removeplayers_@i">
            <div class="modal-dialog modal-sm">
                <div class="modal-content">
                    <div class="modal-header">
                        <h4 class="modal-title">Confirmation needed</h4>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                    <div class="modal-body">
                        <p>Do you really want to delete this record ?</p>
                    </div>
                    <div class="modal-footer justify-content-between">
                        <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>


                        <form asp-action="Match_StatsDelete" asp-route-comp_Id="@item.Match_Stats.Comp_Id" asp-route-team_Id="@item.Match_Stats.Team_Id" asp-route-Match_Id="@item.Match_Stats.Match_Id"
                                asp-route-Match_Stat_Id="@item.Match_Stats.Match_Stat_Id" onsubmit="return jQueryAjaxDeleteDirect(this)" class="d-inline">
                            <button type="submit" class="btn btn-warning btn-xs"><i class="fas fa-trash"></i> Delete Json</button>
                        </form>
                    </div>
                </div>
            </div>
        </div>

    </div>
</td>

  • In my controller - calling the delete :
 public async Task<IActionResult> Match_StatsDelete(string comp_id, string team_id, string match_id, string match_stat_id)
    {
            // Wrap the code in a try/catch block
            try
            {
                var deleteok = _match_statsRepository.Delete(entity);
                if (deleteok != null)
                {
                    //Code Here to populate my table only since this is a partial view inside a page
                    return Json(new { isValid = true, html = JavaScriptConvert.RenderRazorViewToString(this, "_Match_StatsViewAll", myPartialmodelpopulated) });
                }
                else
                {
                    ModelState.AddModelError("", "Could not Delete Match");
                    return RedirectToAction("MatchIndex", new { message = "Success", comp_id });
                }
            }
            catch (Exception ex)
            {
                // Log the exception to a file.We discussed logging to a file
                //......More code here for the log has been removed //
                return View("Error");
            }
    }

Here is the Javascript:

jQueryAjaxDeleteDirect = form => {
    
        try {
            $.ajax({
                type: 'POST',
                url: form.action,
                data: new FormData(form),
                contentType: false,
                processData: false,
                success: function (res) {
                    $('#view-all').html(res.html);
                    $.notify('Deleted Successfully !', {
                        globalPostion: 'Top Center',
                        className: 'success'
                    });
                },
                error: function (err) {
                    console.log(err)
                }
            })
        } catch (ex) {
            console.log(ex)
        }
    

    //prevent default form submit event
    return false;
}

The " return Json" call works fine if I use the original code (so no MODAL form - just the javascript "confirm") . Thank you for the help!

  • Does this screen show after `$('#view-all').html(res.html);` in ajax? What's the `view-all`? I can't find anything about this in your code. There's too much relevant code missing in your question, I can't reproduce the same error, So you need to debug to find out which step will cause this problem – Xinran Shen May 09 '22 at 07:55
  • everything goes well, until :->> return Json(new { isValid = true, html = JavaScriptConvert.RenderRazorViewToString(this, "_Match_StatsViewAll", myPartialmodelpopulated) }); <-- is reached. Then the screen rendered is the image attached above. – lbrettsinclair May 10 '22 at 09:45
  • the "view-all" is coming from the "index view" Match_StatsIndex.cshtml - `
    @await Html.PartialAsync("_Match_StatsViewAll", Model.Match_StatsForAGame)
    `
    – lbrettsinclair May 10 '22 at 12:46
  • Does this page show after you click the `Delete Json` button? From the code you provided, I don't find anything error, I think you can debug in your ajax to check if the program can access the success function and check the value of `res`. – Xinran Shen May 13 '22 at 09:52
  • See the 1rst screenshot provided of what's shown AFTER the JSON. it's rendering the HTML but like a word document. – lbrettsinclair May 13 '22 at 12:23
  • It seems that the code just return the json to page directly instead of `$('#view-all').html(res.html);`, So i suggest you to check the seccess function in ajax. – Xinran Shen May 16 '22 at 00:55

0 Answers0