简体   繁体   English

页面未加载部分视图 HTML

[英]Page Not Loading PartialView HTML

I have a C# MVC Razor site that I am attempting to add some HTML to once the user presses a button.我有一个 C# MVC Razor 站点,一旦用户按下按钮,我正在尝试添加一些 HTML 站点。 The workflow is as follows: the page loads, the user does some stuff (not related to this action), then at some point they click a button which in turn sends an Ajax call to the server to gather some information.工作流程如下:页面加载,用户做一些事情(与此操作无关),然后在某些时候他们单击一个按钮,该按钮又向服务器发送 Ajax 调用以收集一些信息。 That information is loaded into the ViewBag and then a PartialView is returned.该信息被加载到 ViewBag 中,然后返回 PartialView。

Below is the code for this.下面是这个的代码。 The RowPartial.cshtml is indeed being hit and the ViewBag.Rows item does have greater than 0 records in it but the TEST header is not showing up at all. RowPartial.cshtml 确实被击中,并且 ViewBag.Rows 项目确实有超过 0 条记录,但 TEST header 根本没有出现。 I cannot find it anywhere through the Dev Tools in Chrome.我无法通过 Chrome 中的开发工具在任何地方找到它。 It's hitting the code but not actually adding the HTML to the page.它击中了代码,但实际上并未将 HTML 添加到页面中。 What am I doing wrong?我究竟做错了什么?

Controller Controller

    [HttpPost]
    public ActionResult GetRows()
    {
        ViewBag.Rows = Helpers.GetRows();

        return PartialView("RowPartial");
    }

RowPartial.cshtml RowPartial.cshtml

@if (ViewBag.Rows.Count > 0)
{
   <h3>TEST</h3>
}

Index.cshtml索引.cshtml

<div class="row" style="margin-top: 25px;">
    @Html.Partial("CourseRowPartial")
</div>

You need to add the html response where you are calling the GetRows function.您需要在调用 GetRows function 的地方添加 html 响应。 With vanila js you could use something like this:使用 vanila js,您可以使用以下内容:

document.getElementById("divRows").innerHTML = ajaxResponse;

And instead to have this in index.cshtml而是在 index.cshtml 中有这个

<div class="row" style="margin-top: 25px;">
    @Html.Partial("CourseRowPartial")
</div>

You should have, for example, this例如,您应该有这个

<div class="row" style="margin-top: 25px;" id="divRows"></div>

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

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