2017-10-16 136 views
-2

我有這樣的:編碼字符串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 ...),但沒有任何作品,請你給我一個這樣的手?

謝謝!

+0

你想做什麼?你有什麼嘗試? (這沒有幫助「我嘗試另一種東西(HTMLString,HTML.Raw ...)堅果沒有工作」)) – Shyju

+0

在代碼的頂部我有這個:string top =「

"; and string Bot = "
」;我該怎麼把它轉換成HTML,我試着用HTML.Raw(頂部)和HTML.Encode(頂部),但沒有任何@Shyju –

+0

你想按原樣打印它,還是要將HTML呈現在頁面中? – Shyju

回答

1

顧名思義,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> 
+0

我嘗試使用HTML.Raw並且不工作..代碼片段未呈現。 –

+0

它的確如此。看到這裏的工作小提琴https://dotnetfiddle.net/D5COwj – Shyju

+0

謝謝!我的錯誤是我在HTML.Raw開始時錯過了「@」 –

0

我注意到你的代碼有幾個問題。

     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表明不匹配的標籤已被「固定」,即關閉標籤插入等。其他瀏覽器可能具有類似的功能。