简体   繁体   中英

Using interface on ASP.NET MVC html helper

I have the following method:

public static string UlList(this HtmlHelper helper, List<IEntity> entities, string css)
{
    return "foo";
}

However when i try and call the method

<%= Html.UlList(Model.ProfileRequiredFields, css)%>

I get:

Compiler Error Message: CS1928: 'System.Web.Mvc.HtmlHelper' does not contain a definition for 'UlList' and the best extension method overload 'System.Web.Mvc.HtmlHelpers.UlList(System.Web.Mvc.HtmlHelper, System.Collections.Generic.List, string)' has some invalid arguments

Model.RequiredFields = new List<ProfileRequiredField>();

public class ProfileRequiredField : IEntity
{
    public int Id { get; set; }
    public string Name { get; set; }
}

EDIT I'm using 3.5

What you're trying to do, with List<IEntity> as the parameter type, works only in .NET 4.0 because of the new out keyword.

If you need this to work in lower versions, try the following:

public static string UlList<T>(this HtmlHelper helper, List<T> entities, string css)
    where T : IEntity
{
    return "foo";
}

This replaces IEntity with a generic argument. .NET will automatically resolve this for you, so you don't have to add <ProfileRequiredField> in your code.

请确保您的扩展方法在System.Web.Mvc命名空间中,或者将您的命名空间包括在web.config文件中定义的命名空间列表中。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM