ASP.NET MVC Bootstrap dynamic modal content

16,765

You can use bootstrap modal dialog with ajax to load the details/view partial view result to the modal dialog.

First, add a new css class to the button, which we will use to wire up the click event later to fire up the modal dialog. Also we will generate the url to the details view using the Url.Action html helper method and keep that in html5 data attribute in the button(we will use this for our ajax call later)

@foreach (var item in Model.Questions)
{
  <tr>
    <td>
        <button type="button" class="btn btn-success btn-sm  modal-link"  
                 data-targeturl="@Url.Action("Details","Home",new { id = item.Id })">
            View
        </button>
     </td>
   </tr>
}

Now add the below code to your current view (or even the layout). This will act as the container for our modal dialog.

<!-- The below code is for the modal dialog -->
<div id="modal-container" class="modal fade" tabindex="-1" role="dialog">
    <a href="#close" title="Close" class="modal-close-btn">X</a>
    <div class="modal-content">
        <div class="modal-body"></div>
    </div>
</div>

Now, let's wire up the click event on any element with "modal-link" css class. We added this classes to our buttons earlier.

$(function () {

    $('body').on('click', '.modal-link', function (e) {
        e.preventDefault();

        $("#modal-container").remove();

        $.get($(this).data("targeturl"), function (data) {

            $('<div id="modal-container" class="modal fade">'+
              '<div class="modal-content" id= "modalbody" >'+ 
                    data + '</div></div>').modal();

        });
    });
});

So when user clicks on the button, it reads the value of targeturl data attribute (which is a URL with the item id value in the querystring) and make an ajax call to that URL. In our case, It will make a call to the Details action method. So let's make sure that it returns the partial view result (out modal dialog content)

public ActionResult Details(int id)
{
    // Get the data using the Id and pass the needed data to view.

    var vm = new QuestionViewModel();

    var question = db.Questions.FirstOrDefault(a=>a.Id==id);
    // To do: Add null checks

    vm.Id = question.Id;
    vm.Title = question.Title;
    vm.Description = question.Description;

    return PartialView(vm);
}

And the partial view will have the needed html markup for the modal dialog.

@model YourNameSpace.QuestionViewModel
<div class="modal-content">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
        </button>
        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
    </div>
    <div class="modal-body">

        <h4>@Model.Title</h4>
        <p>@Model.Description</p>
        <p>Some other content for the modal </p>

    </div>
    <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
    </div>
</div>
Share:
16,765
Pow4Pow5
Author by

Pow4Pow5

Updated on June 16, 2022

Comments

  • Pow4Pow5
    Pow4Pow5 almost 2 years

    I am using MVC 5 and I have a <button> for each record in the viewModel as follows:

    <table class="table table-hover">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Questions.Single().QuestionType)
        </th>
        <th>
            @Html.DisplayNameFor(model =>model.Questions.Single().Scheme)
        </th>
    </tr>
    
    @foreach (var item in Model.Questions)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.QuestionType)
            </td>
            <td>
                <button type="button" class="btn btn-success btn-sm" data-toggle="modal" data-target="#myModal" >
                    View
                </button>
            </td>
        </tr>
    }
    

    Foreach (Var item in Model.Questions), there will be a button that opens up a modal. However, I want this modal to load different contents based on the item.id from Model.Questions. How can I do this?