【问题标题】:Calling Partial View into Bootstrap Modal through jQuery?通过jQuery将部分视图调用到Bootstrap Modal?
【发布时间】:2018-10-08 18:41:52
【问题描述】:

我已经查看了 Stack 上关于这个问题的几个答案;但是,我发现的结果都没有解决我的问题。

我要做什么

我需要动态填充 Modal 的内容,但内容包含 Razor 函数(这就是我不使用 JS 填充 Modal 的原因)。我不能有单独的模态,因为如果我在页面上包含两个模态,Razor 函数会相互干扰(一个会被调用,而另一个不会)。模态的控制器与页面控制器不同(页面是索引页面,我使用单独的控制器来更加模块化)

我的尝试

我正在使用我的 cshtml 文件中的 HTMLContent 调用创建模态:

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>");
    }

这里在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()

页面加载后,在我的 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();
    });
});

那么应该指向这里:

        [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
        };
    }

但是,当addLineConfig() 返回时,它返回的是布局(即导航栏和页脚)而不是下面的部分页面。

<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>

我尝试使用$.ajax({}) 调用而不是$.get(),但是使用AJAX,部分页面不会呈现(我没有得到任何返回)。

【问题讨论】:

  • 您可以直接从浏览器或提琴手调用端点,看看结果是否符合您的预期?返回的结果是视图还是布局?
  • 到您的部分目录的路由是否正确?
  • 在 html 文件上放置 @Html.Partial("Partials/Widgets/TrendGraphWidget", ViewData) 会正确返回部分页面。
  • @JoshuaLeonard 无法直接调用端点,但从我上面的评论看来路由是正确的。我也可以在没有ViewData 的情况下拨打上述电话。

标签: c# jquery asp.net .net-core


【解决方案1】:

您是否尝试过使用 $load(),过去它可以很好地为我简单地在 div 中放置一个部分。

这是一个例子。

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

【讨论】:

  • 这对我来说会导致 404 错误。在控制器上找到正确操作的唯一 url 是url="/Widgets?handler=TrendConfig"
  • @KyleShake 我通常不使用 $.GET,但是否可以指定您的 dataType,对于正常的 ajax 调用,您需要说它是您返回的 html。 url = "/Widgets?handler=TrendConfig"; $.ajax({ type: 'GET', dataType: 'html', url: url, success: function (data) { $("#ConfigureBody").html(data); } });
  • 不幸的是,我已经尝试过这种方法,但它没有给我正确的结果。它只返回页面的布局,而不是部分页面本身。
  • @KyleShake 您的 TrendConfig 函数在哪个控制器中?
  • 它在一个单独的页面模型* -Widgets.cshtml.cs - 来自索引页面模型。我刚刚尝试将 TrendConfig 函数放在索引模型中,但它仍然没有返回部分页面;它返回 Index.cshtml 的副本。我们没有使用标准的 MVC。我们正在使用带有附加页面模型的 Razor 页面
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多