2
我有某種形式的一個MVC3的網站,很多重複的部分「的類型參數不能從使用推斷」鑑於定製幫手。所以我試圖爲此做一個幫手。下面的例子形成我發這個互聯網:使用標準幫手
using System;
using System.Linq.Expressions;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.Routing;
namespace Nop.Web.Themes.MyTheme.Extensions
{
public static class FormLineHelper
{
public static MvcHtmlString FormLine<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TProperty[]>> expression,
object htmlAttributes = null)
{
TagBuilder tag = new TagBuilder("tr");
tag.MergeAttributes(new RouteValueDictionary(htmlAttributes), true);
var member = (MemberExpression)expression.Body;
string propertyName = member.Member.Name;
tag.InnerHtml += String.Format("<td class='label'>{0}</label></td><td class='field'>{1}</td><td class='padding'>{2}</td>",
htmlHelper.LabelFor(expression), htmlHelper.EditorFor(expression), htmlHelper.ValidationMessageFor(expression));
return MvcHtmlString.Create(tag.ToString());
}
}
}
這編譯就好了。然而,當我做
@model Nop.Plugin.MyPlugin.Models.ViewModel
@{
Layout = "../Shared/_Root.cshtml";
}
<div class="form">
<div class="form-top"></div>
<div class="form-center">
@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<table>
@Html.FormLine(model => model.FirstName)
</table>
}
</div>
<div class="form-bottom"></div>
</div>
我確信在web.config中包含
<compilation debug="true" targetFramework="4.0">
我得到的錯誤「之類參數不能從使用推斷」。另一個作品similair,但不使用標準助手的助手工作正常。我也試過這樣:
@{ Html.FormLine<ViewModel, string>(model => model.FirstName); }
這給錯誤「無法隱式轉換類型‘字符串’到‘字符串[]’」。
我見過similair問題,但我一直沒能找到答案。那麼我做錯了什麼?
啊該死這是一個狡猾的小錯誤。我完全閱讀了。我盯着這些東西瞎了眼。這確實使用'@(Html.FormLine(model => model.FirstName))' 謝謝! –
PeterB
現在它也可以這樣工作:'@ Html.FormLine(model => model.FirstName)' – PeterB