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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:46:20+00:00 2026-05-29T20:46:20+00:00

The scenario Suppose I have the following two model classes: public class ProductColor {

  • 0

The scenario

Suppose I have the following two model classes:

public class ProductColor
{
    public long Id { get; set; }
    public string Name { get; set; }
}

public class Product
{
    public long Id { get; set; }
    public string Name { get; set; }

    public long ProductColorId { get; set; }
    public virtual ProductColor ProductColor { get; set; }
}

Now in a form for creating a new product, I might have this line for the color field…

@Html.EditorFor(model => model.ProductColor)

I want this to generate a drop-down of colors, so I create a custom editor template…

@model MyProject.Models.ProductColor
@using (var db = new MyProject.Models.MyDbContext())
{
    @Html.DropDownList("", new SelectList(db.ProductColors, "Id", "Name", Model))
}

Up to here, this works. But now if I submit this create form, I get this validation error:

The parameter conversion from type ‘System.String’ to type ‘MyProject.Models.ProductColor’ failed because no type converter can convert between these types.

Of course this is because the HTTP request contains the color ID as a string (e.g. "1") and one would need some code to turn that into an actual ProductColor instance. So I wrote a TypeConverter…

[TypeConverter(typeof(ProductColor.PCTypeConverter))]
public class ProductColor
{
    public long Id { get; set; }
    public string Name { get; set; }

    public class PCTypeConverter : TypeConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            return sourceType == typeof(string) ? true : base.CanConvertFrom(context, sourceType);
        }
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if (value.GetType() == typeof(string))
                using (var db = new MyDbContext())
                    return db.ProductColors.Find(Convert.ToInt64(value));
            return base.ConvertFrom(context, culture, value);
        }
    }
}

Now submitting the request works fine as expected, but the custom editor template no longer does. It just doesn’t get invoked. The system thinks that my type is basically like string and just generates a textbox.

The problem

I cannot get both to work. Either I have a type converter, in which case I don’t get the drop-down (because the custom editor template never gets invoked), or I don’t have a type converter, in which case validation fails when the request is submitted.

What is the correct solution to this?

  • 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-29T20:46:21+00:00Added an answer on May 29, 2026 at 8:46 pm
    • Instead of

      @Html.EditorFor(model => model.ProductColor)
      

      refer to the ID field:

      @Html.EditorFor(model => model.ProductColorId)
      
    • Annotate that ID field with a UIHint attribute containing the name of the type (more precisely, the name of the custom editor template):

      public class Product
      {
          public long Id { get; set; }
          public string Name { get; set; }
      
          [UIHint("ProductColor")]
          public long ProductColorId { get; set; }
          public virtual ProductColor ProductColor { get; set; }
      }
      
    • Change the custom editor template so that it uses long? as the model type:

      @model long?
      @using (var db = new MyProject.Models.MyDbContext())
      {
          @Html.DropDownList("",
              new SelectList(db.ProductColors, "Id", "Name", Model))
      }
      
    • Get rid of the TypeConverter.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have an application with the following code: public class Node { public
Lets suppose I have a scenario with the following model: An Animal table which
Suppose I have the following situation: 9 class Program 10 { 11 public static
I was recently pondering the following scenario: suppose you have a huge database and
I am interested in the following scenario specifically. Suppose you have team that writes
Suppose I have the following two strings containing regular expressions. How do I coalesce
suppose we have the following scenario: 2 users both signal ready to play, the
This might be a novice question. :). Consider the following scenario. Suppose we have
Suppose the following scenario: I have a master database that contains lots of data,
Suppose I have the following senario: Trunk is my main development line where programmers

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.