1
我嘗試在cshtml文件中的App_Code中創建一個Helper。ASP.NET MVC Razor Helper with @HTML in App_Code
// Using's are needed to ensure helpers function correctly.
@using System.Web.Mvc;
@using System.Web.Mvc.Html;
@using System.Web.Mvc.Routing;
@using System.Web.Mvc.Razor;
@functions {
private static WebViewPage page { get { return PageContext.Page as WebViewPage; } }
private static System.Web.Mvc.HtmlHelper<dynamic> html { get { return page.Html; } }
private static UrlHelper url { get { return page.Url; } }
private static dynamic viewBag { get { return page.ViewBag; } }
}
@helper HelperName(Func<dynamic, dynamic> expression)
{
<div class="form-group">
@Html.LabelFor(model => expression, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.EditorFor(model => expression, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => expression)
</div>
</div>
}
我不知道這是否可能。我有一些錯誤:
@Html不知道LabelFor,但我放到了首位
的使用也許Func鍵作爲參數錯誤
這是可能的,請檢查此答案如何在幫助程序方法中使用Html助手:http://stackoverflow.com/a/12826750/1708859 –