4

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
3
  • You would load the view using ajax. Commented Jul 30, 2013 at 1:03
  • Can you post your Model and your Controller Action? Commented Jul 30, 2013 at 3:15
  • The controller is only returning the view a above and there is no model right now. The view is only html right now Commented Jul 30, 2013 at 3:17

1 Answer 1

5

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
        }
Sign up to request clarification or add additional context in comments.

1 Comment

I had to change $('#divPartial').html(data) to $('#divPartial').append(data) to get it work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.