Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Bootstrap Modal window as PartialView

I was looking to using the Twitter Bootstrap Modal windows as a partial view. However, I do not really think that it was designed to be used in this fashion; it seems like it was meant to be used in a fairly static fashion. Nevertheless, I think it'd be cool to be able to use it as a partial view.

So for example, let's say I have a list of Games. Upon clicking on a link for a given game, I'd like to request data from the server and then display information about that game in a modal window "over the top of" the present page.

I've done a little bit of research and found this post which is similar but not quite the same.

Has anyone tried this with success or failure? Anyone have something on jsFiddle or some source they'd be willing to share?

Thanks for your help.

like image 834
KWondra Avatar asked Jun 27 '12 17:06

KWondra


People also ask

How do I use Bootstrap partial view?

In order to add Partial View, you will need to Right Click inside the Controller class and click on the Add View option in order to create a View for the Controller.

How do you load a partial view from a modal popup during runtime?

modal-body"). html(res); // Show the modal $mymodal. modal("show"); }); }); }); Now you have to make sure that your Index action method returns a partial view (so that it will not execute any layout code, but just that view code).

How do I use Bootstrap popup modal?

To trigger the modal window, you need to use a button or a link. Then include the two data-* attributes: data-toggle="modal" opens the modal window. data-target="#myModal" points to the id of the modal.

How do I toggle a Bootstrap modal?

Use the . modal(“toggle”) method in Bootstrap to toggle the modal.


1 Answers

Yes we have done this.

In your Index.cshtml you'll have something like..

<div id='gameModal' class='modal hide fade in' data-url='@Url.Action("GetGameListing")'>    <div id='gameContainer'>    </div> </div>  <button id='showGame'>Show Game Listing</button> 

Then in JS for the same page (inlined or in a separate file you'll have something like this..

$(document).ready(function() {    $('#showGame').click(function() {         var url = $('#gameModal').data('url');          $.get(url, function(data) {             $('#gameContainer').html(data);              $('#gameModal').modal('show');         });    }); }); 

With a method on your controller that looks like this..

[HttpGet] public ActionResult GetGameListing() {    var model = // do whatever you need to get your model    return PartialView(model); } 

You will of course need a view called GetGameListing.cshtml inside of your Views folder..

like image 158
Shane Courtrille Avatar answered Oct 22 '22 09:10

Shane Courtrille