How to use Modal in a view?

Coreysan 1,816 Reputation points
2023-11-06T19:49:24.2233333+00:00

I learned to build my first web store with MVC. When a customer wants detail information on a particular product, I used the controller/action method and view technique, where I pass the product ID to a new view, using asp-route-id. Works great.

Now I want to try it differently. I have some HTML that displays the product, and when you click on a details button or icon, it calls modal, from the same html page.

I'd like to see if I can emulate that in MVC. I would need to pass the selected product information to the modal code (div).

Can that be done? I know how to code the basics in C# for MVC, and I know a small amount of Javascript. Learning as I go! Anything I can read/study online?

Developer technologies | ASP.NET | Other
{count} votes

Answer accepted by question author
  1. QiYou-MSFT 4,341 Reputation points Microsoft External Staff
    2023-11-07T09:23:14.8933333+00:00

    Hi @Coreysan

    You can use Bootstrap's modal. You can name a button your item to achieve the function you want.

    <button type="button" data-toggle="modal" data-target="#ProductModal">
      Product1
    </button>
    
    
    
    <div class="modal fade" id="ProductModal" tabindex="-1" role="dialog" aria-labelledby="ProductModalLabel" aria-hidden="true">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
          ....
          </div>
          <div class="modal-body">
            ...
          </div>
          <div class="modal-footer">
            ....
          </div>
        </div>
      </div>
    </div>
    

    I think this document is of great help to you:

    Modal.BootStrap

    Best regards,
    Qi You


    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.


0 additional answers

Sort by: Most helpful

Your answer

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