Click here to Skip to main content
15,895,370 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,
I am developing MVC4 application with Searching, Paging and Sorting facilities using Ajax & Partial View. I had views for CRUD operations opening separately when ActionLink clicks. Everything working fine. Now I want to open them as modal pop up. Whenever user clicks on ActionLink, modal pop up will appear with respective view. User can able to perform operation same as normal view and after completing, modal pop up will disappear refreshing partial view with updated data.

For Create, Edit & Details, I used @Html.ActionLink and for delete, I used @Ajax.ActionLink helpers. I am using strongly typed views.

What is most efficient way to achieve this without making so much changes in existing application.

Updated Question :

My BundleConfig.cs

C#
public static void RegisterBundles(BundleCollection bundles)
        {
            // COMMENTED FOR datepicker ISSSUE. ON Sep 14. 
            //bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
            //            "~/Scripts/jquery-{version}.js"));

            bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include(
                        "~/Scripts/jquery-ui-{version}.js"));

            bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                        "~/Scripts/jquery.unobtrusive*",
                        "~/Scripts/jquery.validate*"));
            
            bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                        "~/Scripts/modernizr-*"));

            bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/site.css"));

            bundles.Add(new StyleBundle("~/Content/themes/base/css").Include(
                        "~/Content/themes/base/jquery.ui.core.css",
                        "~/Content/themes/base/jquery.ui.resizable.css",
                        "~/Content/themes/base/jquery.ui.selectable.css",
                        "~/Content/themes/base/jquery.ui.accordion.css",
                        "~/Content/themes/base/jquery.ui.autocomplete.css",
                        "~/Content/themes/base/jquery.ui.button.css",
                        "~/Content/themes/base/jquery.ui.dialog.css",
                        "~/Content/themes/base/jquery.ui.slider.css",
                        "~/Content/themes/base/jquery.ui.tabs.css",
                        "~/Content/themes/base/jquery.ui.datepicker.css",
                        "~/Content/themes/base/jquery.ui.progressbar.css",
                        "~/Content/themes/base/jquery.ui.theme.css"));
        }


My _Layout.cshtml:

HTML
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width" />
    <title>@ViewBag.Title</title>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
</head>
<body>
    @RenderBody()

    @Scripts.Render("~/bundles/jquery")
    @RenderSection("scripts", required: false)
</body>
</html>


What I have tried:

After googled for solution, I referred [this] but with no luck.
Posted
Updated 27-Sep-16 0:06am
v3

So the way I choose to do it is via jquery utilizing bootstrap. In my view I'll have a link/button and a click event tied to it. Also, in this click event is an ajax call to load the partial and populate it into the body of the modal window.

HTML
@*Lets call this index.cshtml and references a partial called MyModalWindow*@
<script type="text/javascript">
$(function(){ 
    //use .on instead of .click, i'm lazy
    $("#btn-show-modal").click(function(){ 
        $.ajax({
	    cache: false,
            url: '/Controller/ActionToLoadPartial',
	    contentType: 'application/html; charset=utf-8',
            type: 'GET',
            dataType: 'html',
            data: { param: yourParamToKnowWhatToLoad }
        }).success(function (data) {
	    $("#modal-window .modal-body").html(data);
            $("#modal-window").modal("show");
        }).error(function () {
	    // Do something with the error
        });
    });
});
</script>
<a href="javascript:void(0);" id="btn-show-modal">Open</a>

  @Html.Partial("MyModalWindow")


Then I'll have a partial containing the modal window itself

HTML
@*This would be MyModalWindow.cshtml *@
    <script type="text/javascript">
    $(function () {
        $("#modal-window").on("click", "#btn-close", function () {
            $("#modal-window").modal("hide");
        });
    });
</script>

<div id="modal-window" style="display:none;" title="Your Title HEre" class="modal fade">
    <div class="modal-dialog" style="width: 50%;">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title">Title</h4>
            </div>            
			<div class="modal-body">

			</div>
			<div class="modal-footer">
				<button type="button" id="btn-close" class="btn btn-default">Close</button>
			</div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div>


Then in your controller you'll have an action to load the partial (see the first block of code using .ajax. In this example, I have a partial view called _ModalWindowEditTemplate

C#
public ActionResult ActionToLoadPartial(string id, string mode)
{
	try
	{
			// Do something to load any model data
	}
	catch (Exception ex)
	{
		
	}

	return PartialView("_ModalWindowEditTemplate");
}


This is the way I've chosen to do it as I maintain a lot of control over what happens/how things happen. There are many ways to achieve what you are trying to do so if this doesn't work, keep looking on google because this is an extremely common functionality that isn't very unique.
 
Share this answer
 
Comments
Umesh AP 26-Sep-16 2:34am    
Thanks David_Wimbley for your response.
But which jQuery files I need to referred in my view & in which order, Can you please mention.
I am getting different errors because of wrong jQuery files referred.
David_Wimbley 26-Sep-16 9:29am    
Can you use the improve question link on your original question and update it with your bundle.config and your layout.cshtml
C#
bundles.Add(new ScriptBundle("~/Scripts/jquery.unobtrusive-ajax.min.js"));

Add this jquery in BundleConfig.cs file
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900