MVC 5 Razor视图无法在if条件下呈现HTML [英] MVC 5 Razor view not rendering HTML within if condition
问题描述
这是MVC 5剃刀视图代码:ForestView.cshtml
This is the MVC 5 razor view code: ForestView.cshtml
@model Forest.Tree
@{
var resultHtml = string.Empty;
}
<div id="divTreeSearch>
@(Html.Kendo().PanelBar().
Name("panelbar")
.Items(panelbar =>
{panelbar.Add()
.Content(@<text>@Html.Partial("_TreeSearch", Model, ViewData)</text>);}))
</div>
<div id="divTreeSearchResult">
@if(Model.TreeResultObj != null)
{
resultHtml = Html.ContentFromPartial("_TreeReport", Model.TreeResultObj);
@Html.Raw(resultHtml); -- Not working
@Html.Raw(HttpUtility.HtmlDecode(resultHtml)); -- Not Working
Html.Raw(resultHtml); -- Not working
Html.Raw(HttpUtility.HtmlDecode(resultHtml)); -- Not Working
Model.resultStringSaved = resultHtml;
@Html.DisplayText("resultStringSaved"); -- Not Working
@Html.Raw("<text>Test</text>") -- Even this is not working
}
@Html.Raw(Model.resultStringSaved) -- Not Working
@Html.Raw(HttpUtility.HtmlDecode(Model.resultStringSaved)) -- Not Working
@Html.DisplayText("resultStringSaved") -- Not Working
@Html.Raw("<text>Test</text>") -- This is Working
</div>
ForestView.cshtml-@model Forest.Tree _TreeSearch.cshtml-@model Forest.Tree _TreeReport.cshtml-@model Forest.SearchData.Results
ForestView.cshtml - @model Forest.Tree _TreeSearch.cshtml - @model Forest.Tree _TreeReport.cshtml - @model Forest.SearchData.Results
模型Forest.Tree
中的projerty TreeResultObj类型为Forest.SearchData.Results
The projerty TreeResultObj in the model Forest.Tree
is of type Forest.SearchData.Results
ForestView.cshtml
是最初加载的主视图,并显示来自_TreeSearch
部分的搜索输入
输入搜索条件并单击搜索"按钮(所有这些操作均来自_TreeSearch
)-进行ajax调用,并调用TreeSearch(id tree)
动作
The ForestView.cshtml
is the main view which loads initially and displays the search inputs from the _TreeSearch
partial
When search criteria entered and a 'search' button is clicked (all this is from the _TreeSearch
) - a ajax call is make and the TreeSearch(id tree)
action is called
该操作再次返回主"ForestView"-但是现在填充了模型属性"TreeResultObj".因此执行了"ForestView"中"if条件"内的代码,并调用了另一个部分以将内容作为HTML字符串取回,该内容保存在"resultHtml"变量中
The action again returns the main 'ForestView' - however now the model property 'TreeResultObj' is populated. so the code within the 'if conditon' in the 'ForestView' executed and calls another partial to get the content back as HTML string, which is saved in the 'resultHtml' variable
在这一点上,我可以看到类似"<Text>blah blah blah</text>"
的Html Sting
但是,尝试在主"ForestView"中的搜索面板下方显示HTML字符串不起作用-我已经尝试了几乎所有可能的方式.
At this point I can see the Html Sting like "<Text>blah blah blah</text>"
However trying to display the HTML string below the search panel in the main 'ForestView' is not working - I have tried almost every possible way.
如果没有呈现if条件内的任何文本-这是一个ajax调用,因此没有页面刷新-我可以看到HTML字符串值并将其另存为Model属性,但无法在主视图中显示. 任何帮助将非常感激.预先感谢.
Any text within the if condition is not rendered - it is an ajax call so there is no page refresh - I can see the HTML string value and also save it as a Model property but cannot get to display it in the main view. Any help would be much appreciated. Thanks in advance.
推荐答案
此时,您只是在调用方法而忽略结果.试试:
At that point, you are just invoking a method and ignoring the result. Try:
@: @Html.Raw(resultHtml)
@:
切换到输出模式.注意:如果您使用的是明显标记的内容,它将自动切换.例如:
The @:
switches to output mode. Note: if you had used something that was clearly markup, it would have switched automatically. For example:
<div>@Html.Raw(resultHtml)</div>
这篇关于MVC 5 Razor视图无法在if条件下呈现HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!