I am new to ASP.NET Core 6.0 and I am facing an issue while reading a value from .cshtml.cs
file in .cshtml
page.
I have tried to use the [BindProperty]
attribute and also tried to bind value in ViewData
.
Basically, I have created an HTML Table and bound it to a string value. Please find below the sample code that I tried.
cshtml.cs
code:
[BindProperty]
public string HTMLRep { get; set; }
HTMLRep = reportHTML; //reportHTML has the HTML Table in the post ActionMethod
I also tried
ViewData["HTML"] = reportHTML
.cshtml
markup:
@{
var val = ViewData["HTML"];
}
<panel>
@Model.HTMLRep
//Also tried -
@val
<panel>
Any help on this will be very useful. Thank you
CodePudding user response:
By default, if you try to use HTML text for output it encodes it. So you have to write like following:
<panel>
@Html.Raw(Model.HTMLRep)
<panel>
This should fix your issue.
CodePudding user response:
Easiest solution of your problem can be:
If I would you then might be solve this issue in following manner,
Web Site Dynamic Content From Backend:
[HttpGet]
public IActionResult BindDynamicContentToHtmlCard()
{
var convertedHtmlBodyList = new List<string>();
for (int i = 1; i < 10; i )
{
StringBuilder sb = new StringBuilder();
var convertedHtmlBody = "";
sb.Append("<!DOCTYPE html>");
sb.Append("<html>");
sb.Append("<head>");
sb.Append("</head>");
sb.Append("<body>");
sb.Append("<h5 class='card - title' style='border: solid'><strong>TEST WEB SITE " i " </strong></h5>");
sb.Append("<p>Simplest solution of the dynamic card generation.</p>");
sb.Append("<p>This is how you can generate dynamic Card</p>");
sb.Append("<p><strong style='color: red'>Note:</strong> For more details please check details: <a href='https://stackoverflow.com/questions/71167733/i-want-to-use-html-rawstring-where-string-contains-a-complete-html-webpage'>Details</a></p>");
sb.Append("</body>");
sb.Append("</html>");
convertedHtmlBody = sb.ToString();
convertedHtmlBodyList.Add(convertedHtmlBody);
}
ViewBag.bindToHtmlInView = convertedHtmlBodyList;
return View(); ;
}
Panel HTML:
@{
ViewData["Title"] = "BindDynamicToHtml";
}
<div >
<div >
@foreach (var item in ViewBag.bindToHtmlInView)
{
<panel>
@Html.Raw(item)
</panel>
}
</div>
</div>
Panel Output:
Here you can get the panel element. I hope that would help you to achieve your requirement.