简体   繁体   English

部分视图未显示,但其viewmodel在父级上有效

[英]partial view not displaying but its viewmodel works on parent

In the cshtml code header I've instantiated a viewmodel with some data. 在cshtml代码标头中,我使用一些数据实例化了一个viewmodel。 I can bind to and display data from that viewmodel variable in the parent page. 我可以在父页面中绑定到该viewmodel变量并从中显示数据。 However, when I pass the viewmodel variable into a child partial view, nothing displays on the page. 但是,当我将viewmodel变量传递给子局部视图时,页面上没有任何显示。

@{
    Html.Partial("DailyReport", @DailyReportViewModel);
}

Any idea why the child partial view would not be displaying on the page or how to debug this issue? 是否知道为什么子部分视图将不会显示在页面上或如何调试此问题?

Because you are calling the Partial method which return an MvcHtmlString , but you are not using the return value of the method. 因为您正在调用Partial方法,该方法返回MvcHtmlString ,但是没有使用该方法的返回值。

You can do this instead 你可以这样做

@Html.Partial("DailyReport", @DailyReportViewModel);

Razor will now render the return value from the DailyReport method. Razor现在将通过DailyReport方法呈现返回值。

Or you can use the RenderPartial method which render the result. 或者,您可以使用RenderPartial方法呈现结果。

@{
   Html.RenderPartial("DailyReport", DailyReportViewModel);
}

Assuming DailyReportViewModel is an object of type T to which your partial view is strongly typed to. 假设DailyReportViewModel是类型为T的对象, DailyReportViewModel您的部分视图强烈地键入该对象。

@放在最前面,然后从DailyReportViewModel删除@ ,它应该可以工作。

@Html.Partial("DailyReport", DailyReportViewModel);

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

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