使用javascript添加部分视图

Har*_*ock 4 javascript asp.net-mvc

当我单击按钮时,我希望将“ View_Name”的新副本添加到页面中。我将如何在javascipt中执行此操作?以下是页面的开头。

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

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

cha*_*ara 6

见下面的例子

HTML:

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

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

<input type="button" id="mybutton" value="Add new Item" />
</fieldset> 
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

控制器动作:

public ActionResult FilteredItemGallery()
        {

            return PartialView("GalleryPartial");//return your partial view here
        }
Run Code Online (Sandbox Code Playgroud)

  • 我必须将$('#divPartial')。html(data)更改为$('#divPartial')。append(data)才能使其正常工作 (2认同)