【问题标题】:need to render html from mvc, only displaying htmls tags not text需要从mvc渲染html,只显示html标签而不是文本
【发布时间】:2014-05-17 06:19:44
【问题描述】:

我正在使用 razorpdf 在我的 mvc 4 应用程序中生成 pdf 报告。每次我生成pdf时,描述字段都会显示一堆不应该显示的html标签和字段,我只想显示描述字段中的纯文本。有没有人对如何做到这一点有任何建议,还有关于如何将图像添加到 pdf 页面的任何建议?

这是我生成 pdf 的观点:

@model List<ArdentMC.DHS.COP.Mobile.MVC.Models.Reports>
@{
Layout = "";
}
<!DOCTYPE html>
<html>
<head>
    <div style="color=red; align:center;">UNCLASSIFIED/FOR OFFICAL USE ONLY</div>

</head>
<body>

<table style="width:300px;">
   @foreach (var item in Model)
{
<tr>
<td>@item.Phase</td>
<td>@item.NocNumber</td>
<td>@item.Title</td>
<td>@item.Location</td>
</tr>
    <tr<
    <td></td>
    <td></td> 
    <td>@item.ReportDateText</td>
    <td>@item.Description</td>
    </tr>     
}
</table>                             
    <br /><br /><br /><br /><br />
    <br /><br /><br /><br /><br />
     <div style="color:black; align:center;">US Department of Homeland Security</div>
        <br />
        <div style="color:red; align:center;">UNCLASSIFIED/FOR OFFICAL USE ONLY</div>

</body>
</html>

这是我从中提取信息的模型:

          using System;
          using System.Collections.Generic;
          using System.Linq;
          using System.Web;

       namespace ArdentMC.DHS.COP.Mobile.MVC.Models
    {
public class Reports
{
    public string NocNumber { get; set; }
    public string Title { get; set; }
    public Nullable<System.DateTime> IncidentDate { get; set; }
    public string ReportDateText { get; set; }
    public string IncidentType { get; set; }
    public Nullable<int> IncidentSubtypeId { get; set; }
    public string IncidentSubtype { get; set; }
    public Nullable<int> PhaseId { get; set; }
    public string Phase { get; set; }
    public string InitialNotification { get; set; }
    public string Location { get; set; }
    public string NocSpotRep { get; set; }
    public string Contributors { get; set; }
    public string Participants { get; set; }
    public string Description { get; set; }
    public Nullable<int> IncidentStateId { get; set; }
    public string IncidentState { get; set; }
    public Nullable<System.DateTime> PublishDate { get; set; }
    public Nullable<System.DateTime> ArchiveDate { get; set; }

    }
}

有人对如何只显示描述文本有一些建议吗?先感谢您。

【问题讨论】:

  • 您在视图中的 body 标记之外有 HTML,这会导致您的问题吗?
  • 您在循环中创建的行中的列也不匹配,第一行有 4 列,第二行只有 2。将“colspan = 2”添加到第二行或添加更多行。
  • 这些事情都没有影响...我尝试了@html.raw,但这似乎也不起作用,我被告知我需要以某种方式呈现 html,但没有足够的经验来获得它工作....

标签: html asp.net-mvc-4 render razorpdf


【解决方案1】:

我可以添加 @foreach(模型中的变量项){ @Html.Raw(item.Description) } 进入我的视图,并为描述字段呈现正确的代码,因此没有可见的标签,只有文本。

【讨论】:

    【解决方案2】:
    public ActionResult NotFound() {
        Response.ContentType = "text/html";
        return View();
    }
    

    【讨论】:

    • 在这里,答案必须有解释,我不会将其标记为删除 ATM,但我敦促您解释您的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-17
    相关资源
    最近更新 更多