At the top of the page, add usage for System.Linq.Expressions, for example.
@using System.Linq.Expressions
(or add this namespace to web.config).
Then, to create an assistant, it will look something like this:
@helper DisplayField(Expression<Func<MyModel, string>> field)
{
@Html.LabelFor(field)
@Html.DisplayFor(field)
}
, .. , HtmlHelper, , "MyModel" "string", . , , . , :
@helper DisplayField<TModel, TValue>(Expression<Func<TModel, TValue>> field)
{
@Html.LabelFor(field)
@Html.DisplayFor(field)
}
, , HtmlHelper. :
:
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
namespace MvcApplication1.HtmlHelpers
{
public static class HtmlHelpers
{
public static MvcHtmlString DisplayFieldFor<TModel, TValue>(
this HtmlHelper<TModel> helper,
Expression<Func<TModel, TValue>> field)
{
var labelString = helper.LabelFor(field);
var displayString = helper.DisplayFor(field);
return MvcHtmlString.Create(
labelString.ToString() +
displayString.ToString());
}
}
}
:
@Html.DisplayFieldFor(m => m.Name)
@Html.DisplayFieldFor(m => m.PhoneNumber)
... HtmlHelper web.config.