MVC 5 Razor视图不呈现HTML在if条件下

时间:2023-01-16 11:32:31

This is the MVC 5 razor view code: ForestView.cshtml

这是MVC 5 razor视图代码:forest .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森林。树_TreeSearch。cshtml——@ model森林。树_TreeReport。cshtml——@ model Forest.SearchData.Results

The projerty TreeResultObj in the model Forest.Tree is of type Forest.SearchData.Results

森林模型中的树。树的类型为Forest.SearchData.Results

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。cshtml是最初加载并显示_TreeSearch部分的搜索输入的主要视图,当输入搜索条件并单击“search”按钮(所有这些都来自_TreeSearch)时,将进行ajax调用并调用TreeSearch(id树)操作

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

动作再次返回主“ForestView”——然而现在模型属性“TreeResultObj”被填充。因此,在'ForestView'中执行的'if conditon'中的代码并调用另一个部分来将内容作为HTML字符串返回,该字符串保存在'resultHtml'变量中

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.

此时,我可以看到Html的刺痛,比如“ blah blah blah blah ”,但是我试图在搜索面板的“ForestView”中显示Html字符串并没有效果——我尝试了几乎所有可能的方法。

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.

if条件中的任何文本都没有呈现——它是一个ajax调用,因此没有页面刷新——我可以看到HTML字符串值,也可以将其保存为模型属性,但不能在主视图中显示它。如有任何帮助,我们将不胜感激。提前谢谢。

4 个解决方案

#1


1  

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>

#2


0  

1 - And the content from render should be violating HTML syntax and Razor normally doesn't render wrong HTML content. 2 - Enclose the HTML.Raw(resultHtml) into dev.

1 -渲染的内容应该违反HTML语法,Razor通常不会渲染错误的HTML内容。2 -将HTML.Raw(resultHtml)封装到dev中。

#3


0  

After zillions of tests in a full-day: Change use of:

在一天内完成无数个测试之后:改变使用:

@Html.Raw(Html.DisplayFor(modelItem => item.Mensagem)) 

by:

由:

@Html.Raw(Html.DisplayTextFor(modelItem => item.Mensagem))

You will get the rendered HTML withou any <>!!!

您将获得任何<>!!

#4


0  

I was also facing the same issue but I could make it to working. I wanted to open a div start and conditionally end it within a for loop.

我也面临着同样的问题,但我可以让它发挥作用。我想打开一个div start,并在for循环中有条件地结束它。

Put any dummy HTML element before the @Html.Raw

在@Html.Raw之前放置任何虚拟HTML元素

<div id="divTreeSearchResult">
@if(Model.TreeResultObj != null)
 {
  resultHtml = Html.ContentFromPartial("_TreeReport", Model.TreeResultObj);
  <span>dummySpan</span>
  @Html.Raw(resultHtml);    
  @Html.Raw(HttpUtility.HtmlDecode(resultHtml)); 
  @Html.Raw(resultHtml);   
  @Html.Raw(HttpUtility.HtmlDecode(resultHtml)); 

 Model.resultStringSaved  = resultHtml;
 @Html.DisplayText("resultStringSaved"); 

 @Html.Raw("<text>Test</text>") 

}

@Html.Raw(Model.resultStringSaved) 
@Html.Raw(HttpUtility.HtmlDecode(Model.resultStringSaved)) 
@Html.DisplayText("resultStringSaved") 

@Html.Raw("<text>Test</text>") 
</div>

After you place a dummy html element there all the below @Html.Raw will work

在您放置一个虚拟的html元素之后,所有下面的@Html。生会工作

Happy Coding

快乐的编码

Tarak

塔拉

#1


1  

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>

#2


0  

1 - And the content from render should be violating HTML syntax and Razor normally doesn't render wrong HTML content. 2 - Enclose the HTML.Raw(resultHtml) into dev.

1 -渲染的内容应该违反HTML语法,Razor通常不会渲染错误的HTML内容。2 -将HTML.Raw(resultHtml)封装到dev中。

#3


0  

After zillions of tests in a full-day: Change use of:

在一天内完成无数个测试之后:改变使用:

@Html.Raw(Html.DisplayFor(modelItem => item.Mensagem)) 

by:

由:

@Html.Raw(Html.DisplayTextFor(modelItem => item.Mensagem))

You will get the rendered HTML withou any <>!!!

您将获得任何<>!!

#4


0  

I was also facing the same issue but I could make it to working. I wanted to open a div start and conditionally end it within a for loop.

我也面临着同样的问题,但我可以让它发挥作用。我想打开一个div start,并在for循环中有条件地结束它。

Put any dummy HTML element before the @Html.Raw

在@Html.Raw之前放置任何虚拟HTML元素

<div id="divTreeSearchResult">
@if(Model.TreeResultObj != null)
 {
  resultHtml = Html.ContentFromPartial("_TreeReport", Model.TreeResultObj);
  <span>dummySpan</span>
  @Html.Raw(resultHtml);    
  @Html.Raw(HttpUtility.HtmlDecode(resultHtml)); 
  @Html.Raw(resultHtml);   
  @Html.Raw(HttpUtility.HtmlDecode(resultHtml)); 

 Model.resultStringSaved  = resultHtml;
 @Html.DisplayText("resultStringSaved"); 

 @Html.Raw("<text>Test</text>") 

}

@Html.Raw(Model.resultStringSaved) 
@Html.Raw(HttpUtility.HtmlDecode(Model.resultStringSaved)) 
@Html.DisplayText("resultStringSaved") 

@Html.Raw("<text>Test</text>") 
</div>

After you place a dummy html element there all the below @Html.Raw will work

在您放置一个虚拟的html元素之后,所有下面的@Html。生会工作

Happy Coding

快乐的编码

Tarak

塔拉