简体   繁体   English

通过jQuery将部分视图调用到Bootstrap模式中?

[英]Calling Partial View into Bootstrap Modal through jQuery?

I've viewed several of the answers on Stack about this problem; 我已经查看了Stack上有关此问题的几个答案; however, none of the results I've found have solved my problem. 但是,我发现的结果都没有解决我的问题。

What I'm trying to do 我想做什么

I need to dynamically populate a Modal's content but the content contains Razor functions (which is the reason I'm not using JS to populate the Modal). 我需要动态填充Modal的内容,但是内容包含Razor函数(这就是我不使用JS填充Modal的原因)。 I can't have separate modals because the Razor functions will interfere with each other (one will be called while the other will not be) if I include both Modals on the page. 我不能有单独的模态,因为如果我在页面上同时包含两个模态,则Razor函数会相互干扰(一个将被调用,而另一个则不会)。 The Controller for the Modals is different from the Page Controller (The page is the Index page & I'm using a separate controller to be more modular) 模式控制器与页面控制器不同(该页面为索引页面,我使用一个单独的控制器来实现更高的模块化)

My attempt 我的尝试

I am creating the Modal using an HTMLContent call in my cshtml file: 我正在使用cshtml文件中的HTMLContent调用来创建Modal:

Widgets.cs Widgets.cs

        public static IHtmlContent ConfigWidgetModal()
    {

        return new HtmlString("<div class='modal fade' id='configure-widget' tabindex='-1' role='dialog' aria-labelledby='myLargeModalLabel' aria-hidden='true'>" +
                                    $"<div class='modal-dialog modal-lg' role='document'>"+
                                        $"<div class='modal-content'>" +
                                            $"<div class='modal-header'>" +
                                                $"<h5 class='modal-title'>Select Widget Information</h5>"+
                                                $"<button type = 'button' class='close' data-dismiss='modal'>&times;</button>"+
                                            $"</div>"+

                                            $"<div class='modal-body' id='ConfigureBody'>" +
                                                //JavaScript will set the Body depending on the widget type being selected
                                            $"</div>" +
                                            $"<div class='modal-footer'>" +

                                            $"</div>" +
                                        $"</div>" +
                                    $"</div>" +
                                $"</div>");
    }

It is called here in Index.cshtml 它在Index.cshtml中称为

<div>... Index body up here </div>

@*Grid for Widgets*@
@Widgets.Dashboard()

@*Edit/Add/Stop Editing Widget Buttons*@
@Widgets.AddEditButtons()

@* Initial Widget Popup *@
@{ string pagetype = "LP"; }
@Widgets.AddWidgetModal(pagetype)

@* Widget Configuration Popup*@
@Widgets.ConfigWidgetModal()

Once the page loads, in my Widgets.js file I'm calling this: 页面加载后,在我的Widgets.js文件中,我将其称为:

 $("#addLine").on('click', function () {
    var url = "/Widgets?handler=TrendConfig";
    var addLineConfig = function () {
        var url = "/Widgets?handler=TrendConfig";
        return $.get(url, function (data) {
            $("#ConfigureBody").html(data);
        });
    }
    addLineConfig().done(function () {
        makeLineWidget();
    });
});

Which then should be directing here: 然后应该将其引导至此处:

        [HttpGet]
    public ActionResult TrendConfig(string adid)
    {
        ViewData["adid"] = adid;
        var myViewData = new Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary(ViewData);
        return new PartialViewResult
        {
            ViewName = "/Partials/Widgets/TrendGraphWidget",
            ViewData = myViewData
        };
    }

However, when addLineConfig() returns, it is returning the Layout (ie the Navbar and the Footer) not the Partial page below. 但是,当addLineConfig()返回时,它将返回布局(即Navbar和页脚),而不是下面的Partial页面。

<script>
//Changes plot options for Line Graph dependent on Activity selection
...some script here about the dropdowns...
</script>
<div class="form-row justify-content-center">
<div class="col-md-3">
    @MyGlobalDropdowns.FinCenter()
</div>
<div class="col-md-3">
    @MyGlobalDropdowns.Client()
</div>

I've tried using an $.ajax({}) call instead of the $.get() but with AJAX, the partial page doesn't render (I don't get anything returned). 我尝试使用$.ajax({})而不是$.get() $.ajax({})调用,但使用AJAX时,部分页面无法呈现(我什么也没有得到返回)。

Have you tried using $load(), it has worked well for simply placing a partial in a div for me in the past. 您是否尝试过使用$ load(),它过去曾经为我简单地将部分放置在div中的效果很好。

here is an example. 这是一个例子。

$("#ConfigureBody").load("/Controller/TrendConfig/?adid=value");

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

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