How to build dynamic href inside a modal in a MVC View

Coreysan 1,811 Reputation points
2023-12-28T20:44:43.53+00:00

I have an MVC Core project with a view that calls a modal.

I pass the database data to Javascript, build some elements, then use the elements in the modal. For example,

<div class="product" id="ProductId"></div>

where ProductId was created in a Javascript function with getElementByID.

I was hoping to use the ProductId value with "asp-route-id=ProductId" but I know that won't work, so how can I build a dynamic href using ProductId?

Something like:

<a href=/Home/AddToCart/[and use ProductID here]></a>

Can that be done somehow?

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,815 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,598 questions
0 comments No comments
{count} votes

Accepted answer
  1. Lan Huang-MSFT 30,181 Reputation points Microsoft External Staff
    2023-12-29T05:22:33.1+00:00

    Hi @Coreysan,

    You can set the data-id to Model.ProductId and it will get the id of the clicked anchor and display the corresponding data in the mode (detailed view) on the screen.

     <a href="#" id="btnEdit" class="btn btn-primary btn-sm" data-id="@item.ProductId">Edit</a>
    

    Edit

    View

    Index

    @model List<CoreMvc.Models.Product>
    
    
    @{
        Layout = null;
    }
    
    <!DOCTYPE html>
    
    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Index</title>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" />
        <script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
        <script type="text/javascript">
            $(function () {
                $("[id*=BtnEdit]").click(function () {
                    var productId = $(this).attr('data-id');
                    $.ajax({
                        type: 'POST',
                        url: '/Home/AddToCart',
                        data: { id: productId }
                    }).done(function (response) {
                        $("#actionCon").html(response);
                        $("#myModalEdit").modal('show');
                    }).fail(function (XMLHttpRequest, textStatus, errorThrown) {
                        debugger;
                        alert("Fail");
                    });
                });
            });
        </script>
    </head>
    <body>
        <table class="table table-striped">
            <thead>
                <tr>
                    <td>Id</td>
                    <td>Name</td>
                    <td>Data</td>
                </tr>
            </thead>
            <tbody>
                @foreach (var item in Model)
                {
                    <tr>
                        <td>@item.ProductId</td>
                        <td>@item.Name</td>
                        <td>@item.Data</td>
                        <td>
                            <button type="button" id="BtnEdit" data-id="@item.ProductId">AddToCart</button>
                        </td>
                    </tr>
                }
            </tbody>
        </table>
        <div id="actionCon"></div>
    </body>
    </html>
    

    AddToCart

    @model CoreMvc.Models.Product
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" />
    <script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
    
    <form id="EditCat">
        <div class="container">
            <!-- The Modal -->
            <div class="modal fade" id="myModalEdit">
                <div class="modal-dialog modal-dialog-centered">
                    <div class="modal-content">
                        <!-- Modal Header -->
                        <div class="modal-header">
                            <h4 class="modal-title">Edit Category</h4>
                            <button type="button" class="close" data-dismiss="modal">&times;</button>
                        </div>
                        <!-- Modal body -->
                        <div class="modal-body">
                            <input id="hfId" type="hidden" name="ID" value="@Model.ProductId" />
                            <div class="form-group">
                                <input type="text" value="@Model.Name" class="form-control" id="txtName" name="Name" placeholder="Enter Name" />
                            </div>
                            <br />
                            <div class="form-group">
                                <input type="text" value="@Model.Data" class="form-control" id="txtData" name="Name" placeholder="Enter Country" />
                            </div>
                        </div>
                        <!-- Modal footer -->
                        <div class="modal-footer">
                            <a asp-controller="Home" asp-action="Add" asp-route-id="@Model.ProductId">Add</a>
                            <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </form>
    

    Model

     public class Product
     {
         public int ProductId { set; get; }
         public string Name { set; get; }
         public string Data { set; get; }
       
     }
    
     public IActionResult Index()
     {
        
         List<Product> Products = GetProducts(null);
    
         return View(Products);
     }
    
    
     [HttpPost]
     public IActionResult AddToCart(int id)
     {
         Product product = new Product();
         product.ProductId = id;
         product.Name = GetProducts(id).FirstOrDefault().Name;
         product.Data = GetProducts(id).FirstOrDefault().Data;
         return PartialView(product);
     }
    
    
    
    
     [HttpGet("AddToCart/{id}")]
     public IActionResult Add([FromRoute] int id)
     {
         Product product = new Product();
         product.ProductId = id;
         //....
         return Json(product);
     }
    

    Best regards,

    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


6 additional answers

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  3. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  4. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.