【问题标题】:asp.net core mvc, make view that all come from string object's html code as a stringasp.net core mvc,将所有来自字符串对象的html代码作为字符串查看
【发布时间】:2021-10-21 06:42:11
【问题描述】:

我想制作一个模型,从动态字符串对象中获取所有 html 代码。像这样:

动态 html 模型:

public class PageHtmlModel
    {
        public string HtmlCode { get; set; }
    }

实体模型

public class Entity
    {
        private int _ID;
        private string _Module;
        private string _Explanation;
        private string _Situation;
        private string _Personel;
        private string _PersonelsNotes;
        private string _EndDate;
        public int id { get => _ID; set => _ID = value; }
        public string module { get => _Module; set => _Module = value; }
        public string explanation { get => _Explanation; set => _Explanation = value; }
        public string situation { get => _Situation; set => _Situation = value; }
        public string personel { get => _Personel; set => _Personel = value; }
        public string personels_notes { get => _PersonelsNotes; set => _PersonelsNotes = value; }
        public string end_date { get => _EndDate; set => _EndDate = value; }
    }

DataAccesLayer:

public static string html()
        {
            PageHtmlModel pageHtmlModel = new PageHtmlModel();
            //Thats All html page comes from string
            pageHtmlModel.HtmlCode = "@model deneme100.Models.Entity <html> <head> <link href='~/css/invoice.css' rel='stylesheet' /> </head> <body> <div class='index-box'> <table cellpadding='0' cellspacing='0'> <tr class='top'> <td colspan='2'> <table> <tr> <td class='title'> <img src='~/img/logo.png' style='width:100%; max-width:300px;' /> </td> <td> @Html.DisplayNameFor(model => model.id):@Html.Raw(Model.id) <br>@Html.DisplayNameFor(model => model.module):@Html.Raw(Model.module) <br>@Html.DisplayNameFor(model => model.explanation):@Html.Raw(Model.explanation) </td> </tr> </table> </td> </tr> <tr class='information '> <td colspan='2 '> <table> <tr> <td> @Html.DisplayNameFor(model => model.situation):@Html.Raw(Model.situation) <br>@Html.DisplayNameFor(model => model.end_date):@Html.Raw(Model.end_date) </td> <td> </td> </tr> </table> </td> </tr> <tr class='heading '> <td> @Html.DisplayNameFor(model => model.personels_notes) </td> <td></td> </tr> <tr class='item'> <td> @Html.Raw(Model.personels_notes) </td> </tr> <tr class='total '> <td></td> <td> @Html.DisplayNameFor(model => model.personel):@Html.Raw(Model.personel) </td> </tr> </table> </div> </body> </html>";
            return (pageHtmlModel.HtmlCode);
        }

这是我打开其他动态模型页面的索引页面

//This is catching object id for entity object
 
$('#myTable tbody').on('click', '#pdf', function () {
            if (!($(this).closest('tr').hasClass('selected'))) {
                table.$('tr.selected').removeClass('selected');
                $(this).closest('tr').addClass('selected');
            }
            obj = {}
            obj.id = $('tr.selected td').eq(0).html();
            window.location = "/Home/PDF/" + obj.id;            
            if ($(this).closest('tr').hasClass('selected')) {
                $(this).closest('tr').removeClass('selected');
            }

        })

控制器

        public IActionResult PDF(int id)
        {
             var result = new EntityDataAccess.DataObject();
             result.data = EntityDataAccess.OneObject(new Entity() { id = id });
             return View(EntityDataAccess.html(), result.data[0]);
        }

查看 => 我只想写这个并查看我的所有 html 页面

@model myProject.Models.PageHtmlModel
@{
    ViewData["Title"] = "PDF";
    Layout = null;
}

感谢您从现在开始的帮助

【问题讨论】:

  • 还有什么可以帮助您的吗?

标签: model-view-controller model asp.net-core-mvc asp.net-core-3.1 jsreport


【解决方案1】:

好吧,你可以使用 @Html.Raw() HTML 助手来绑定你的 dynamic HTML 在你的视图上。这是给你的例子。

控制器动作:

[HttpGet]
public IActionResult BindDynamicToHtml()
{
    
    StringBuilder sb = new StringBuilder();
    var convertedHtmlBody = "";
    sb.Append("<!DOCTYPE html>");
    sb.Append("<html>");
    sb.Append("<head>");
    sb.Append("</head>");

    sb.Append("<body>");
    sb.Append("<p>Dear Coder,</p>");
    sb.Append("<p>Please check the following answer. This is how you can generate dynamic HTML to View</p>");
    sb.Append("<div style='max - height:500px; overflow: auto; width: 100 %; border - spacing: 0; '>");
    sb.Append("<style> table, th, td { border: 1px solid black; } th{ padding:2px; } </style>");
    sb.Append("<table style='width:20%;'>");
    sb.Append("<tr>");
    sb.Append("<th style='color:red;text-align:left;'>Test Bindings</th>");
    sb.Append("<th style='text-align:right'>40</th>");
    sb.Append("</tr>");
    sb.Append("</table>");
    sb.Append("<br />");
    sb.Append("<table style='width:20%;text-align:center;'>");
    sb.Append("<tr style='color:red;'>");
    sb.Append("<th style='text-align:center'>Name</th>");
    sb.Append("<th style='text-align:center'>Violations</th>");
    sb.Append("<th style='text-align:center'>Details</th>");
    sb.Append("</tr>");
    sb.Append("</table>");
    sb.Append("</div>");
    sb.Append("<hr/>");
    sb.Append("<p><strong style='color: red'>Note:</strong> For more details please check details: <a href='https://stackoverflow.com/users/9663070/md-farid-uddin-kiron'>Details</a></p>");
    sb.Append("</body>");
    sb.Append("</html>");
    convertedHtmlBody = sb.ToString();

    ViewBag.bindToHtmlInView = convertedHtmlBody;
    return View(); ;
}

查看:

@{
    ViewData["Title"] = "BindDynamicToHtml";
}

@Html.Raw(ViewBag.bindToHtmlInView)

输出:

注意:如果您想了解更多有关 HTML 助手功能的信息,可以参考我们的 official document herethis one

希望它能帮助您解决问题。

【讨论】:

  • 它有效,但我无法访问文件中的链接。通常,如果我使用剃刀语法,我可以看到。
  • 你终于回来了,好吧,我明白了你的意思,为了清楚这个问题,我已经向你展示了如何做到这一点。因此,如果您发布有关此问题的新问题,那将是很好的,以便我也可以指导您这样做,我们的两个上下文保持分开。希望你明白我的意思。在需要设置一些额外方法的情况下,也可以访问链接。
猜你喜欢
  • 1970-01-01
  • 2018-03-27
  • 1970-01-01
  • 2012-07-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-13
  • 2011-09-28
相关资源
最近更新 更多