编码字符串HTML Asp.NET MVC剃刀
我有这样的:编码字符串HTML Asp.NET MVC剃刀
@model IEnumerable<MVPTracker.Models.DashboardResult>
@using MVPTracker.Models;
@{
int row = 0;
ViewBag.Title = "DashboardList";
Layout = "~/Views/Shared/_Layout.cshtml";
string top = "<div class='row'> ";
string Bot = "</div> ";
}
<div class="wrapper wrapper-content animated fadeInRight">
<div class="row">
<div class="col-lg-12">
<div class="ibox float-e-margins">
<div class="ibox-content">
<div class="wrapper wrapper-content animated fadeInRight">
@{
foreach (var item in Model)
{
if (row == 0)
{
Html.Encode(top);
}
<div class="col-md-3">
<div class="ibox">
<div class="ibox-content product-box">
<div class="product-imitation">
<img src="~/Images/App/EventHeader.jpg" width="100%" />
</div>
<div class="product-desc">
<span class="product-price">
@Html.DisplayFor(modelItem => item.Sport.Name)
</span>
<a href="@Url.Action("ViewResults", "Results", new { key = Functions.encrypt(item.DashboardResultID.ToString()) })" class="product-name"> @Html.DisplayFor(modelItem => item.EventName)</a>
<div class="small m-t-xs">
<strong>Desc:</strong> @Html.DisplayFor(modelItem => item.Description) <br />
<strong>Date:</strong> @Html.DisplayFor(modelItem => item.EventDate)
</div>
<br />
<div class="m-t text-righ">
<a href="@Url.Action("UploadResults", "Results", new { key = Functions.encrypt(item.DashboardResultID.ToString()) })" class="btn btn-xs btn-outline btn-primary">Upload Results <i class="fa fa-long-arrow-right"></i> </a>
<a href="@Url.Action("ViewResults", "Results", new { key = Functions.encrypt(item.DashboardResultID.ToString()) })" class="btn btn-xs btn-outline btn-primary">View Results <i class="fa fa-long-arrow-right"></i> </a>
</div>
</div>
</div>
</div>
</div>
if (row == 3)
{
Html.Encode(Bot);
row = 0;
}
else
{
row++;
}
}
}
</div>
</div>
</div>
</div>
</div>
我有2个变量有一个div片段,我当行打印开放股利和股利接近已经完整,实际的代码我不能做到这一点,我尝试另一种东西(HTMLString,HTML.Raw ...),但没有任何作品,请你给我一个这样的手?
谢谢!
顾名思义,Html.Encode
方法将对输入进行编码。如果您试图将变量中的HTML呈现为页面的HTML,则可以使用Html.Raw
方法。
Html.Raw方法不会执行html编码。因此,请确保您将安全的HTML传递到此页面以防止可能的脚本注入。
<div class="wrapper wrapper-content animated fadeInRight">
@foreach (var item in Model)
{
if (row == 0)
{
@Html.Raw(top);
}
<div>some html</div>
if (row == 0)
{
@Html.Raw(Bot);
}
row++;
}
</div>
我尝试使用HTML.Raw并且不工作..代码片段未呈现。 –
它的确如此。看到这里的工作小提琴https://dotnetfiddle.net/D5COwj – Shyju
谢谢!我的错误是我在HTML.Raw开始时错过了“@” –
我注意到你的代码有几个问题。
在
if (row == 0)
{
Html.Encode(top);
}
你不C#和标记模式,这样的结果之间切换(错误的调用)Html.Encode()
丢失。
更改此代码
if (row == 0)
{
<text>@Html.Raw(top)</text>
}
这同样适用于与if (row == 3)
开头的代码实现。
接下来,你似乎没有关闭开口top
<div>
的for
环后如果循环使用的row != 3
值结束。
您应该始终使用View source code
或其他类似来检查呈现的HTML。我注意到Firefox表明不匹配的标签已被“固定”,即关闭标签插入等。其他浏览器可能具有类似的功能。
你想做什么?你有什么尝试? (这没有帮助“我尝试另一种东西(HTMLString,HTML.Raw ...)坚果没有工作”)) – Shyju
在代码的顶部我有这个:string top =“
你想按原样打印它,还是要将HTML呈现在页面中? – Shyju