小编Ami*_*mit的帖子

无法从使用mvc3中推断出类型参数

我试图使用扩展方法的强类型视图

<% using (Html.BeginForm()) { %>
    <%: Html.ValidationSummary(true) %>
    <fieldset>
        <legend>MyUsageViewModel</legend>

        <div class="editor-label">
            <%= Html.LabelFor(model => model.test) %>
        </div>   
                              <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
<% } %>
    <div>
    <%: Html.ActionLink("Back to List", "Index") %>
</div>
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

无法从用法中推断出方法'System.Web.Mvc.Html.LabelExtensions.LabelFor(System.Web.Mvc.HtmlHelper,System.Linq.Expressions.Expression>)'的类型参数.尝试显式指定类型参数.

帮助将不胜感激

c# asp.net-mvc-3

9
推荐指数
1
解决办法
3409
查看次数

TResponse必须是非抽象类型,带有无参数构造函数,以便将其用作参数TResponse

我正在尝试创建一个类似的函数

        public static TResponse Run<TService, TResponse>(Controller mvcController,            IServiceController serviceController, Func<TService, TResponse> action, bool suppressErrors)
        where TService : ICommunicationObject
        where TResponse : ResponseBase<TResponse>
    {
        TResponse response = serviceController.Run<TService, TResponse>(action);
        if (!suppressErrors)
            response.Result.Errors.ToList().ForEach(i => mvcController.ModelState.AddModelError(UniqueKey.ValidationMessage, i.Message));
        return response;
    }
Run Code Online (Sandbox Code Playgroud)

和类被定义为

[DataContract]
public class ResponseBase<T> where T: new()
{
    public ResponseBase()
    {
        Result = new Result<T>();
    }

    [DataMember]
    public Result<T> Result { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我收到编译错误,因为TResponse必须是非抽象类型,带有无参数构造函数才能将其用作参数TResponse

任何帮助,将不胜感激..

c# generics

2
推荐指数
1
解决办法
3605
查看次数

标签 统计

c# ×2

asp.net-mvc-3 ×1

generics ×1