为对象创建HTML帮助器

时间:2018-06-22 12:41:53

标签: c# razor html-helper

您好,我真的很困惑,然后再次成为我的第一个html助手(我没有捏过)。

基本上,我有一个对象和一些选项,然后我想显示带有一些选项的标签。

到目前为止,我的代码是

public static IHtmlString DocumentModalLink(this HtmlHelper htmlHelper, CesaDocument  doc, string title, string model, string style, string inner)
        {
            var builder = new TagBuilder("a");

            builder.MergeAttribute("title", title);
            builder.MergeAttribute("href", "javascript:;");
            builder.MergeAttribute("data-toggle", "model");
            builder.MergeAttribute("data-target", model);
            builder.AddCssClass("btn");
            builder.AddCssClass("btn-" + style);

            builder.InnerHtml = inner;

            builder.MergeAttribute("data-docid", doc.Id.ToString());
            builder.MergeAttribute("data-docdate", htmlHelper.DisplayFor(x => doc.DocDate).ToString());
            builder.MergeAttribute("data-client", htmlHelper.DisplayFor(x => doc.Surname).ToString() + ", " + htmlHelper.DisplayFor(x =>  doc.Forename).ToString());
            builder.MergeAttribute("data-lastmoddate", htmlHelper.DisplayFor(x => doc.Modified).ToString());
            builder.MergeAttribute("data-lastmoduser", htmlHelper.DisplayFor(x => doc.ModifiedBy.Value).ToString());
            builder.MergeAttribute("data-docname", htmlHelper.DisplayFor(x => doc.Name).ToString());
            builder.MergeAttribute("data-docsection", htmlHelper.DisplayFor(x => doc.SectionLookup.Value).ToString());
            builder.MergeAttribute("data-businessunit", htmlHelper.DisplayFor(x =>  doc.BusinessUnitLookup.Value).ToString());
            builder.MergeAttribute("data-doctitle", htmlHelper.DisplayFor(x =>  doc.DocTitle.Value).ToString());


            return MvcHtmlString.Create(builder.ToString());




        }

我可以使用@Html.DocumentModalLink(d, "Notification", "#NotificationsCreateModal","primary", "<span class=\"glyphicon glyphicon-bell\"></span>")

但这不起作用。 DisplayFor仅适用于我不知道如何成功使用的表达式。

我的真正问题是要正确格式化的docDate。 所以基本上是的,如何在多个字段的帮助器中使用DisplayFor。

谢谢

1 个答案:

答案 0 :(得分:0)

您为什么要使用DisplayFor(x => doc.DocDate)?这将使用HTML或其他显示模板(如果该类型存在)显示该值。

您只想将值呈现到属性中。就像使用doc.DocDate

一样,使用doc.Id等。