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 7435959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:09:45+00:00 2026-05-29T10:09:45+00:00

I originally created an editor template like this @model MyModel var items = //

  • 0

I originally created an editor template like this

@model MyModel
var items = // get items
@Html.DropDownListFor(m => m.Id, items, new { id = Html.IdFor(m => m) })

which was invoked by

@Html.EditorFor(m => m.SomeClass)

where SomeClass has an Id property. (The IdFor is one of my HTML helpers).

This would generate something like this

<select name="SomeClass.Id" id="SomeClass" />

Now I want to change the editor template into an HTML helper, so that my call looks like this

@Html.CustomEditorFor(m => m.SomeClass)

I’m changing this from a view to helper because its easier for reusability.

This is loosely what I have:

public static MvcHtmlString CustomEditorFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression)
{
    var idString = htmlHelper.IdFor(expression);
    var propertyValue = expression.Compile()(htmlHelper.ViewData.Model);
    var items = // get items
    return htmlHelper.DropDownListFor(expression, items, new {id = idString});
}

However, when I call the editor, I get this HTML instead of what I want.

<select name="SomeClass" id="SomeClass" />

How can I modify the expression enough to allow it to “access” the Id property?

  • 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-29T10:09:46+00:00Added an answer on May 29, 2026 at 10:09 am

    Try this:

    public static MvcHtmlString CustomEditorFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression)
    {
        var idString = htmlHelper.IdFor(expression);
        var items = // get items
    
        var param = Expression.Parameter(typeof(TModel), "m");
        var member = Expression.Property(
                         Expression.Property(param, ExpressionHelper.GetExpressionText(expression))
                     , "Id");
    
        var isNullable = Nullable.GetUnderlyingType(member.Type);
        if (isNullable != null) {
            var expr2 = Expression.Lambda<Func<TModel, int?>>(
                            member, new[] { param }
                        );
    
            return htmlHelper.DropDownListFor(expr2, items, new { id = idString });
        }
    
        var expr = Expression.Lambda<Func<TModel, int>>(
                       member, new[] { param }
                   );
    
        return htmlHelper.DropDownListFor(expr, items, new { id = idString });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a custom editor template for DateTimeOffset: @model DateTimeOffset <div> @Html.LabelFor(m =>
I originally created a process that occurred by transitioning between views in a multiview
I'm currently turning an array of pixel values (originally created with a java.awt.image.PixelGrabber object)
I created a REST webservice in WCF that originally used POST to perform any
I'm originally a Java programmer who now works with Objective-C. I'd like to create
quick back story-- I am working on ASP.Net based template editor that lets authors
I had originally created a windows form to be a dialog of my projects
When I originally created a Dynamic Data project I told it to expose the
I have an old website originally created in MS Frontpage that I'm trying to
I have a sqlite table that was originally created with: PRIMARY KEY (`column`); I

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.