简体   繁体   English

使用Ajax渲染部分视图的问题

[英]Problem with rendering partial view using Ajax

I have successfully rendered partial view using jQuery.ajax my code is: 我已经使用jQuery.ajax成功渲染了部分视图,我的代码是:

Html HTML

<div id="product-pop-up" style="display: none; width: 700px;">
    <div class="product-page product-pop-up">
        <div class="row" id="modalBody">

        </div>
    </div>
</div>

jQuery jQuery的

$(".button-first-view").click(function () {
    var productId = $(this).data("id");

    $.ajax({
        url: "/Product/FastViewProduct",
        data: {
            "productId": productId
        },
        contentType: "application/html; charset=utf-8",
        type: "GET",
        cache: !0,
        datatype: "html",
        success: function (t) {
            $("#modalBody").html(t);
        },
        error: function () {
            $("#modalBody").html("some things wrong");
        }
    });
});

And my controller 还有我的控制器

public async Task<ActionResult> FastViewProduct(int productId)
{
    var quickView = await _objProductDal.GetProductForQuickView(productId);

    if (quickView.ProductName == null)
    {
        ViewBag.ErrorMessage = "Something wrong, No product Found";
    }

    return PartialView("_FastViewProductPartial", quickView);
}

The above code can successfully return html and css, but I lost my javascript effect. 上面的代码可以成功返回html和css,但是我失去了JavaScript效果。 If I use HTML.RenderAction() this can return partial view with javascript effect. 如果我使用HTML.RenderAction()则可以返回具有javascript效果的部分视图。 But in my case I cannot use HTML.RenderAction . 但就我而言,我不能使用HTML.RenderAction How can I solve this problem? 我怎么解决这个问题?

As you are returning modal whole html as partial view, you will need to call the modal("show") on the modal div element to show it: 当您返回模态整个html作为部分视图时,您将需要在modal div元素上调用modal("show")来显示它:

success: function (t) {
                    $("#modalBody").html(t);
                    $("#product-pop-up").modal("show");
                },

The approach i normally do is put the modal on the parent view ie the main view not partial view : 我通常做的方法是将模态放在父视图上,即主视图而不是局部视图:

<div class="modal fade" id="modal-id" role="dialog" aria-labelledby="" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Title</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true" class="la la-remove"></span>
                </button>
            </div>
            <div class="modal-body" id="modal-body-id">
            </div>

        </div>
    </div>
</div>

and in ajax call success push the html from partial view as content of popup body and call .modal("show") : 在ajax调用成功中,将HTML的部分视图作为弹出式正文的内容推入HTML并调用.modal("show")

 success: function (t) {
                    $("#modal-body-id").html(t);
                    $("#modal-id").modal("show");
                },

or we can put the following attributes on the button, in that case the popup should be on the page already which means it should be already on the main view: 或者我们可以在按钮上放置以下属性,在这种情况下,弹出窗口应该已经在页面上,这意味着它应该已经在主视图上:

<button type="button" class="btn btn-primary" 
        data-toggle="modal" data-target="#modal-body-id">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM