1

I have three projects under one solution: website (razor pages), model and a class library for creating static HTML documents. For now I only really need the model and the library to be used inside the website, but I separated them in case I need to reuse them later.

I would like to use Html.DisplayNameFor() and Html.DisplayFor() inside the class library, since I have already given Display and DataType attributes to the model. The problem is that those methods are inside Microsoft.AspNetCore.Mvc.Rendering namespace.

I tried finding the NuGet package, but only found that Microsoft.AspNetCore.Mvc.ViewFeatures is in 2.2.0 version and it also installs .Http, .Authorization, and other stuff that I don't really need. I found get the value of DisplayName attribute, but it only works for the name and I believe it would be cumbersome to write something similar for every DataType as well.

How can I get those two methods inside library? Or could I just merge the library into the website project that has already everything?

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Dawid Wysocki
  • 61
  • 1
  • 3
  • After you build your whole solution all the required libs would be side to the application and I think if you reference what you need, it would not be a problem. – Babak Fakhriloo Jul 12 '21 at 12:41
  • The accepted answer to the linked question includes the generic `GetAttribute` function, which lets you get any attribute of any type. You can see how it is used in the `GetPropertyDisplayName` function. It should be easy to copy/paste that `GetPropertyDisplayName` function and replace `DisplayName` with `Display` to create a `GetPropertyDisplay` function. Even better if you step through the code and attempt to understand what the code is doing... – Heretic Monkey Jul 12 '21 at 12:53
  • Well problem really rises when I need to implement ```DisplayFor()```. It uses ```DataType``` attribute to change the string output. For example it returns only a date string from ```DateTime``` with attribute ```[DataType(DataType.Date)]``` . – Dawid Wysocki Jul 12 '21 at 12:59

0 Answers0