为对象创建 html 帮助程序



嗨,我真的被这个难住了,然后又是我的第一个 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 仅适用于我不知道如何成功使用的表达式。

我真正的问题是我想正确格式化的文档日期。 所以基本上是的,如何在多个字段的帮助程序中使用 DisplayFor。

谢谢

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

您只想将值呈现为属性。请改用doc.DocDate等,就像使用doc.Id一样

最新更新