0

Hi I'm really stumped on this one, then again it's one of my first html helpers (that I haven't pinched).

Basically I have an object and a few options which I then want to display a a tag with a few options.

my code so far is

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());




        }

and I can call that using @Html.DocumentModalLink(d, "Notification", "#NotificationsCreateModal","primary", "<span class=\"glyphicon glyphicon-bell\"></span>")

but that doesn't work. The DisplayFor only work for expressions which I don't know how to use successfully.

My true problems are the docDate which I want to format correctly. So basically yeah how to use DisplayFor within a helper for multiple fields.

Thanks

Richard Housham
  • 1,525
  • 2
  • 15
  • 31
  • You can use `Display()` instead of `DisplayFor()`. Alternatively, change the extension method to accept an expression. But creating an extension method for this is overkill (it only accepts one specific model which is not what extension methods are for) so you should be just creating a `DisplayTemplate` for your model –  Jun 22 '18 at 13:55
  • By DisplayTemplate - you mean a partial View? If that's the case then whats the best way of adding parameter to them? Currently there doesn't seem to be a good way apart from using the viewBag. I just can't seem to get a handle on when to use which. – Richard Housham Jun 22 '18 at 14:57

1 Answers1

0

Why are you using DisplayFor(x => doc.DocDate)? That renders the value using HTML or another displaytemplate if it exists for that type.

You just want to render the value into an attribute. Use doc.DocDate and so on instead, just as you do with doc.Id

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • because I want to get the format right of the date - i.e the one I've specified in the object. The problem is that it doesn't work. – Richard Housham Jun 22 '18 at 12:53