c# - How to write style using Html.Raw in ASP.NET MVC - Stack Overflow

admin2025-04-25  2

I am trying to write a style using Html.Raw in an ASP.NET MVC view but it is not working. If I check page source, then that particular style is not there in HTML source in <head>.

I have tried the following methods:

<style>
@{
    Html.Raw("table.dataTable tbody td { white-space: nowrap; }");
}
</style>

and

@{
    Html.Raw("<style> table.dataTable tbody td { white-space: nowrap; } </style>");
}

I also tried Html.Display but same issue. How can I display style like this?

I am trying to write a style using Html.Raw in an ASP.NET MVC view but it is not working. If I check page source, then that particular style is not there in HTML source in <head>.

I have tried the following methods:

<style>
@{
    Html.Raw("table.dataTable tbody td { white-space: nowrap; }");
}
</style>

and

@{
    Html.Raw("<style> table.dataTable tbody td { white-space: nowrap; } </style>");
}

I also tried Html.Display but same issue. How can I display style like this?

Share Improve this question edited Jan 21 at 1:41 Rena 36.9k7 gold badges48 silver badges87 bronze badges asked Jan 15 at 6:40 Frank MartinFrank Martin 3,46116 gold badges54 silver badges81 bronze badges 3
  • 1 you can just added <style>...</style> in your page with out razor. – Maytham Fahmi Commented Jan 15 at 8:09
  • 2 This question is similar to: Razor - HTML.RAW does not output text. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. – Lã Ngọc Hải Commented Jan 15 at 8:38
  • Html.Raw returns IHtmlString, an interface which can be used to write the content into the response. But you've then ignored the return value. – Jeremy Lakeman Commented Jan 21 at 1:56
Add a comment  | 

1 Answer 1

Reset to default -1

You can define div tag and apply style in it. In the @Html.Raw tag , no need of @{} to render the tag.

转载请注明原文地址:http://anycun.com/QandA/1745596333a90956.html