So, let's say I have a small model object that contains the required string and has a maximum length of 50:
public class ObjectModel { [Required] [MaxLength(50)] public string Name { get; set; } }
I need to create a special HTML helper where I can pass a string (in this case ObjectModel.Name), and if necessary, create an HTML input element with the class "required".
Now I am trying to work with:
public static HtmlString Input(string label) { return new HtmlString("<input type=\"text\" />"); }
So, in my Razor view, if I do something like @InputHelper.Input(Model.Name) , I cannot access the attributes. My question is, how can I structure my HTML helper class to accept the Model property along with my attributes?
So, I have made further progress, but I still have not enough experience to navigate through the expressions to get what I want. Right now I have:
@InputHelper.Input(m => Model.Title.TitleName, "titlename2", "Title Name")
The second and third parameters are not relevant to this issue. And in the helper method, I have:
public static HtmlString Input(Expression<Func<string, Object>> expression, string id, string label)
But when I move on to debugging the code, there are so many objects and properties that I donβt know where my Required and MaxLength attributes are, even if they are there.