0

I want to delete a record from table and I have to send the ID of that record to the modal, how I do this?

main View :

<tbody>
      @foreach (var item in Model)
         {
           <tr>
                <td> @rowCount</td>
                <td>@item.CategoryTitle</td>
                <td>@item.IconCategory</td>
                <td>
                   <a data-toggle="modal" data-target="#Deletemodal" asp-route- 
                   category="@item.CategoryId" title="delete" class="btn btn-danger btn- 
                   xs">delete</a>
                 </td>
            </tr>
                       
         }
  </tbody>

Partial view as a Modal:

@await Html.PartialAsync("_CreateMainCategory")
3
  • You can try to dynamically load partial view in modal in ajax success callback function. Commented Apr 5, 2021 at 5:07
  • thanks, can u help me with code? I'm not good in ajax Commented Apr 5, 2021 at 5:12
  • can u help me with code? I'm not good in ajax Hi @AliKhansari, you can check the sample code that I shared. Commented Apr 5, 2021 at 5:19

1 Answer 1

2

I want to delete a record from table and I have to send the ID of that record to the modal, how I do this?

To achieve your requirement, you can try to dynamically load partial view in your modal, like below.

<a data-toggle="modal" data-target="#Deletemodal" title="delete" class="btn btn-danger btn-xs" 
   onclick="Func_LoadPv(@item.CategoryId)">delete</a>

Html code of modal popup

<div id="Deletemodal" class="modal fade" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title">Modal Header</h4>
            </div>
            <div class="modal-body">
                @*modal body here*@
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
        </div>

    </div>
</div>

JS function

<script>
    function Func_LoadPv(Cid) {
        $.get("/Home/ShowPartialView?CategoryId=" + Cid, function (res) {
            $("div.modal-body").append(res);
        });
    }
</script>

Action method

public IActionResult ShowPartialView(int CategoryId)
{
    return PartialView("_CreateMainCategory", CategoryId);
}
Sign up to request clarification or add additional context in comments.

10 Comments

The modal opens but does not enter the controller $.get("/AdminPanel/Category/ShowPartialView?CategoryId=" + Cid, function (res) { $("div.modal-body").append(res); }); AdminPanel is area
there is error in Console : Uncaught SyntaxError: Invalid or unexpected token
Uncaught SyntaxError: Invalid or unexpected token In Console tab, you can try to find which code snippet cause the error.
Displays the <a data-toggle="modal" data-target="#Deletemodal" onclick="Func_LoadPv(59ccfd4a-39cd-4c1a-813b-0405b05d0eb4)" class="btn btn-danger btn-xs">delete</a> tag as an error
Ok, it seems that the CategoryId you passed would be a Guid, you can try to modify the code as onclick="Func_LoadPv('@item.CategoryId')". And modify your action method accept a Guid-type parameter.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.