不能同时使用TypeConverter和自定义显示/编辑器模板吗? [英] Cannot use TypeConverter and custom display/editor template together?

查看:111
本文介绍了不能同时使用TypeConverter和自定义显示/编辑器模板吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下两个模型类:

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:

从类型"System.String"到类型"MyProject.Models.ProductColor"的参数转换失败,因为没有类型转换器可以在这些类型之间进行转换.

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

当然,这是因为HTTP请求包含颜色ID作为字符串(例如"1"),并且需要一些代码才能将其转换为实际的ProductColor实例.所以我写了一个TypeConverter ...

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.

我不能两个都上班.我有一个类型转换器,在这种情况下,我没有得到下拉列表(因为自定义编辑器模板从未被调用),或者我没有类型转换器,在这种情况下,提交请求时验证失败

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?

推荐答案

  • 代替

    • Instead of

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

      请参阅ID字段:

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

    • 使用UIHint属性注释该ID字段,其中包含类型名称(更确切地说,是自定义编辑器模板的名称):

    • 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; }
      }
      

    • 更改自定义编辑器模板,以使其使用long?作为模型类型:

    • 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))
      }
      

    • 摆脱TypeConverter.

    • Get rid of the TypeConverter.

      这篇关于不能同时使用TypeConverter和自定义显示/编辑器模板吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆