user751651
user751651

Reputation:

Adding Partial view with javascript

When I click the button I want a new copy of the "View_Name" to be added to the page. How would I do this in javascipt? Below is what the page looks like to start with.

<fieldset>
 <legend>Some Form</legend>

@{ Html.RenderPartial("Partiale"); }
<input type="button" value="Add new Item" />
</fieldset

Upvotes: 4

Views: 7446

Answers (1)

chamara
chamara

Reputation: 12709

See below example

HTML:

<fieldset>
 <legend>Some Form</legend>

<div id="divPartial">
</div>

<input type="button" id="mybutton" value="Add new Item" />
</fieldset> 

Jquery:

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> 
<script>
    $(document).ready(function () {

            $("#mybutton").click(function () {

                var url = "/Item/FilteredItemGallery/"; 

                $.ajax({
                    url: url,
                    cache: false,
                    type: "POST",
                    success: function (data) {


                        $('#divPartial').html(data);
                        /* little fade in effect */
                        $('#divPartial').fadeIn('fast');

                    },
                    error: function (reponse) {
                        alert("error : " + reponse);
                    }
                });

            });

        });
</script>

Controller Action:

public ActionResult FilteredItemGallery()
        {

            return PartialView("GalleryPartial");//return your partial view here
        }

Upvotes: 5

Related Questions