Render Partial View Using jQuery in ASP.NET MVC

后端 未结 8 2043
有刺的猬
有刺的猬 2020-11-22 11:13

How do I render the partial view using jquery?

We can render the partial View like this:

<% Html.RenderPartial(\"UserDetails\"); %>


        
相关标签:
8条回答
  • 2020-11-22 11:45

    Another thing you can try (based on tvanfosson's answer) is this:

    <div class="renderaction fade-in" 
        data-actionurl="@Url.Action("details","user", new { id = Model.ID } )"></div>
    

    And then in the scripts section of your page:

    <script type="text/javascript">
        $(function () {
            $(".renderaction").each(function (i, n) {
                var $n = $(n),
                    url = $n.attr('data-actionurl'),
                    $this = $(this);
    
                $.get(url, function (data) {
                    $this.html(data);
                });
            });
        });
    
    </script>
    

    This renders your @Html.RenderAction using ajax.

    And to make it all fansy sjmansy you can add a fade-in effect using this css:

    /* make keyframes that tell the start state and the end state of our object */
    @-webkit-keyframes fadeIn { from { opacity:0; } to { opacity:1; } }
    @-moz-keyframes fadeIn { from { opacity:0; } to { opacity:1; } }
    @keyframes fadeIn { from { opacity:0; } to { opacity:1; } }
    
    .fade-in {
        opacity: 0; /* make things invisible upon start */
        -webkit-animation: fadeIn ease-in 1; /* call our keyframe named fadeIn, use animattion ease-in and repeat it only 1 time */
        -moz-animation: fadeIn ease-in 1;
        -o-animation: fadeIn ease-in 1;
        animation: fadeIn ease-in 1;
        -webkit-animation-fill-mode: forwards; /* this makes sure that after animation is done we remain at the last keyframe value (opacity: 1)*/
        -o-animation-fill-mode: forwards;
        animation-fill-mode: forwards;
        -webkit-animation-duration: 1s;
        -moz-animation-duration: 1s;
        -o-animation-duration: 1s;
        animation-duration: 1s;
    }
    

    Man I love mvc :-)

    0 讨论(0)
  • 2020-11-22 11:45

    If you need to reference a dynamically generated value you can also append query string paramters after the @URL.Action like so:

        var id = $(this).attr('id');
        var value = $(this).attr('value');
        $('#user_content').load('@Url.Action("UserDetails","User")?Param1=' + id + "&Param2=" + value);
    
    
        public ActionResult Details( int id, string value )
        {
            var model = GetFooModel();
            if (Request.IsAjaxRequest())
            {
                return PartialView( "UserDetails", model );
            }
            return View(model);
        }
    
    0 讨论(0)
提交回复
热议问题