C# 为对象创建html帮助程序

C# 为对象创建html帮助程序,c#,razor,html-helper,C#,Razor,Html Helper,嗨,我真的被这一个难住了,然后再次它是我的第一个html助手之一(我没有捏) 基本上我有一个对象和几个选项,然后我想显示一个带有几个选项的标签 到目前为止,我的代码是 public static IHtmlString DocumentModalLink(this HtmlHelper htmlHelper, CesaDocument doc, string title, string model, string style, string inner) {

嗨,我真的被这一个难住了,然后再次它是我的第一个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,“通知”,“notificationsCreateModel”,“主”来调用它)

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

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


谢谢

为什么要使用
DisplayFor(x=>doc.DocDate)
?使用HTML或其他displaytemplate(如果该类型存在)呈现值


您只需要将值渲染到属性中。使用
doc.DocDate
等等,就像使用
doc.Id

一样,因为我希望获得日期的正确格式,即我在对象中指定的格式。问题是它不起作用。您可以使用
Display()
而不是
DisplayFor()
。或者,更改扩展方法以接受表达式。但是为此创建一个扩展方法是过分的(它只接受一个特定的模型,而扩展方法并不适用于此),所以您应该只为您的modelBy DisplayTemplate创建一个
DisplayTemplate
——您是指局部视图吗?如果是这样的话,那么向它们添加参数的最佳方法是什么?目前,除了使用viewBag之外,似乎没有什么好办法。我只是不知道什么时候该用它。