MVC 5 Razor view not rendering HTML within if condition

烂漫一生 提交于 2019-12-14 01:24:08

问题


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

The projerty TreeResultObj in the model Forest.Tree is of type 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

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

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.

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.


回答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:


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.

Put any dummy HTML element before the @Html.Raw

<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

Happy Coding

Tarak




回答3:


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.




回答4:


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



来源:https://stackoverflow.com/questions/41251838/mvc-5-razor-view-not-rendering-html-within-if-condition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!