Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6811021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:16:35+00:00 2026-05-26T20:16:35+00:00

I have a model (simplified for relevance) like this: public abstract class TitleCreateModel :

  • 0

I have a model (simplified for relevance) like this:

public abstract class TitleCreateModel : ICreateModel
{
    [Required]
    [MaxLength(400)]
    public string TitleName { get; set; }

    [Required]
    [MaxLength(4)]
    public DateTime ReleaseDate { get; set; }

    [Required]
    [MaxLength(5)]
    public string Test { get; set; }

    [Required]
    [MaxLength(2)]
    public int Wut { get; set; }
}

Then I have a custom HTML helper class and an Expression extension class (both unabridged):

public class InputHelper
{
    public static HtmlString Input<T>(Expression<Func<T, Object>> expression, string id, string label)
    {
        var req = expression.GetAttribute<T, Object, RequiredAttribute>();
        var max = expression.GetAttribute<T, Object, MaxLengthAttribute>();
        var required = "";
        var maxlength = "";
        if(req!=null)
        {
            required = "req";
        }
        if(max!=null)
        {
            maxlength = "maxlength='" + max.Length + "'";
        }
        return new HtmlString("<div class=\"clearfix\"><label for=\""+id+"\">" + label + "</label>" +
                              "<div class=\"input\"><input id=\""+id+"\" class=\""+required+"\" type=\"text\" "+maxlength+"/></div></div>");
    }
}

public static class ExpressionExtensions
{
    public static TAttribute GetAttribute<TIn, TOut, TAttribute>(this Expression<Func<TIn, TOut>> expression) where TAttribute : Attribute
    {
        var memberExpression = expression.Body as MemberExpression;
        if (memberExpression != null)
        {
            var attributes = memberExpression.Member.GetCustomAttributes(typeof(TAttribute), true);
            return attributes.Length > 0 ? attributes[0] as TAttribute : null;
        }
        return null;
    }
}

In my Razor script, I make the following calls:

@(InputHelper.Input<string>(m => Model.Title.TitleName, "titlename", "Title Name"))

@(InputHelper.Input<string>(m => Model.Title.Test, "testfield", "Test Field"))

@(InputHelper.Input<int>(m => Model.Title.Wut, "tester", "Test Field 2"))

@(InputHelper.Input<DateTime>(m => Model.Title.ReleaseDate, "release_year", "Release Year"))

For some reason, the GetAttribute method only finds the attributes for TitleName and Test, both of which are string properties for TitleCreateModel. It’s unable to find the attributes for ReleaseDate and Wut, and I have no idea why.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:16:36+00:00Added an answer on May 26, 2026 at 8:16 pm

    Modify the expression used in Input method to Expression<Func<T, TOut>> expression

    public static HtmlString Input<T, TOut>(Expression<Func<T, TOut>> expression, string id, string label)
    {
        var req = expression.GetAttribute<T, TOut, RequiredAttribute>();
        var max = expression.GetAttribute<T, TOut, MaxLengthAttribute>();
        var required = "";
        var maxlength = "";
        if(req!=null)
        {
            required = "req";
        }
        if(max!=null)
        {
            maxlength = "maxlength='" + max.Length + "'";
        }
        return new HtmlString("<div class=\"clearfix\"><label for=\""+id+"\">" + label + "</label>" +
                              "<div class=\"input\"><input id=\""+id+"\" class=\""+required+"\" type=\"text\" "+maxlength+"/></div></div>");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a model like this defined... public class Product{ private long timestamp; //
I have models that look like this (simplified of course): class Product(models.Model): product_code =
I have the following model (simplified): abstract class CartItem { EntityReference<Cart> Cart; } class
I have this simplified model: class Item(models.Model): name = models.CharField(max_length=120) class ItemImage(models.Model): image =
I have the following model class (simplified) class A{ private String name; private List<B>
I have the following model (greatly simplified for the purposes of this question): class
I have 3 django models (simplified for this example): class Fighter (models.Model): name =
I have models (simplified example): class Group(models.Model): name = models.CharField(max_length = 32) class Person(models.Model):
I have the following (heavily simplified) model, that uses will_paginate class Search < ActiveRecord::Base
In this simplified scenario, I have a model called Post Each post has a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.