C# 为自定义EditTemplate(Ajax)实现扩展System.String

C# 为自定义EditTemplate(Ajax)实现扩展System.String,c#,.net,asp.net-mvc,vb.net,asp.net-mvc-3,C#,.net,Asp.net Mvc,Vb.net,Asp.net Mvc 3,这是以下内容 通常,当我有一个城市时,我会使用一些javascript自动完成用户类型中的城市 现在我想让它使用edittemplate自动生成。所以我会改变: Public String City{get;set;} 到 但是我不能继承System.String类,所以我希望使用最好的替代方法。 我将创建City类的新视图,并在我的EditorTemplate中添加javascript,这样我就不必再添加它了:) 提前感谢。我假设您想要的是使用Html.EditorFor()方法及其同级,

这是以下内容

通常,当我有一个城市时,我会使用一些javascript自动完成用户类型中的城市

现在我想让它使用edittemplate自动生成。所以我会改变:

 Public String City{get;set;}

但是我不能继承System.String类,所以我希望使用最好的替代方法。 我将创建City类的新视图,并在我的EditorTemplate中添加javascript,这样我就不必再添加它了:)


提前感谢。

我假设您想要的是使用
Html.EditorFor()
方法及其同级,它对简单类型不太适用。您可以将
City
类隐式转换为
System.String
,并且它应该与实际的
String
无法区分:

public class City
{
    private readonly string city;

    public City(string city)
    {
        if (city == null)
            throw new ArgumentNullException("city");

        this.city = city;
    }

    public override bool Equals(object obj)
    {
        City other = obj as City;
        return (other != null) && this.city.Equals(other.city);
    }

    public override int GetHashCode()
    {
        return this.city.GetHashCode();
    }

    public override string ToString()
    {
        return this.city;
    }

    public static implicit operator string(City city)
    {
        return city.city;
    }

    public static implicit operator City(string city)
    {
        return new City(city);
    }
}
现在可以将此类传递给需要
System.String的方法,反之亦然:

string city = new City("Washington");
City city = "Washington";

我假设您想要的是使用
Html.EditorFor()
方法及其同级,这对于简单类型来说不太好。您可以将
City
类隐式转换为
System.String
,并且它应该与实际的
String
无法区分:

public class City
{
    private readonly string city;

    public City(string city)
    {
        if (city == null)
            throw new ArgumentNullException("city");

        this.city = city;
    }

    public override bool Equals(object obj)
    {
        City other = obj as City;
        return (other != null) && this.city.Equals(other.city);
    }

    public override int GetHashCode()
    {
        return this.city.GetHashCode();
    }

    public override string ToString()
    {
        return this.city;
    }

    public static implicit operator string(City city)
    {
        return city.city;
    }

    public static implicit operator City(string city)
    {
        return new City(city);
    }
}
现在可以将此类传递给需要
System.String的方法,反之亦然:

string city = new City("Washington");
City city = "Washington";

我不会为一个新类型而烦恼,而是在模型上使用元数据来更改编辑器。属性上的
[UIHint(“MyCityTemplate”)]
属性将使EditorFor和DisplayFor使用MyCityTemplate,而不是字符串的默认编辑器模板。

我不想麻烦使用新类型,而是在模型上使用元数据来更改编辑器。属性上的
[UIHint(“MyCityTemplate”)]
属性将使EditorFor和DisplayFor使用MyCityTemplate,而不是字符串的默认编辑器模板。

我没有关注你的问题。我没有关注你的问题。我已经考虑过实现toString方法,但我想没有更好的办法了。谢谢我已经考虑过实现toString方法,但我认为没有更好的方法了。谢谢回答得很好,这符合我的问题。我必须承认,这是不公平的,但我的字面问题是实现System.String,如果我问得不同的话(改变不同显示模板的最佳方式,…我已经给了你最好的答案)。对不起,不过是+1!回答得很好,这符合我的问题。我必须承认,这是不公平的,但我的字面问题是实现System.String,如果我问得不同的话(改变不同显示模板的最佳方式,…我已经给了你最好的答案)。对不起,不过是+1!