C# 如何在MVC中扩展TextArea for
我知道如何扩展TextBoxFor:C# 如何在MVC中扩展TextArea for,c#,asp.net-mvc,asp.net-mvc-3,asp.net-mvc-4,extension-methods,C#,Asp.net Mvc,Asp.net Mvc 3,Asp.net Mvc 4,Extension Methods,我知道如何扩展TextBoxFor: public static MvcHtmlString TextBoxFor<TModel, TValue>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TValue>> expression, object htmlAttributes) { MvcHtmlString html = defau
public static MvcHtmlString TextBoxFor<TModel, TValue>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TValue>> expression, object htmlAttributes)
{
MvcHtmlString html = default(MvcHtmlString);
RouteValueDictionary routeValues = new RouteValueDictionary(htmlAttributes);
html = System.Web.Mvc.Html.InputExtensions.TextBoxFor(htmlHelper, expression, routeValues);
return html;
}
public static MvcHtmlString TextBoxFor(此HtmlHelper HtmlHelper、表达式、对象htmlAttributes)
{
MvcHtmlString html=默认值(MvcHtmlString);
RouteValueDictionary routeValues=新的RouteValueDictionary(htmlAttributes);
html=System.Web.Mvc.html.InputExtensions.TextBoxFor(htmlHelper、表达式、RouteValue);
返回html;
}
我想对TextAreaFor执行同样的操作,但不幸的是,System.Web.Mvc.Html.InputExtensions不包含TextAreaFor方法。如何解决此问题?请参见,它位于TextAreaExtensions
静态类中
return System.Web.Mvc.Html.TextAreaExtensions.TextAreaFor(htmlHelper, expression, routeValues);
或者只是
return htmlHelper.TextAreaFor(expression, routeValues);
顺便说一句,第三个参数只是(在TextBoxFor中)一个IDictionary HtmlatAttributes
,与RouteValue无关
它之所以能工作,是因为RouteValueDictionary
实现了IDictionary
,但您的参数令人困惑。请参见,它位于TextAreaExtensions
静态类中
return System.Web.Mvc.Html.TextAreaExtensions.TextAreaFor(htmlHelper, expression, routeValues);
或者只是
return htmlHelper.TextAreaFor(expression, routeValues);
顺便说一句,第三个参数只是(在TextBoxFor中)一个IDictionary HtmlatAttributes
,与RouteValue无关
这只是因为
RouteValueDictionary
实现了IDictionary
,但您的参数很混乱。如果您使用[System.ComponentModel.DataAnnotations.DataType(DataType.MultilineText)]
标记属性,然后使用编辑器for
,您应该会得到想要的结果
public class SomeClass
{
[DataType(DataType.MultilineText)]
public string Name { get; set; }
}
@Html.EditorFor(x => x.Name)
如果您使用
[System.ComponentModel.DataAnnotations.DataType(DataType.multilitext)]
标记属性,然后使用EditorFor
您应该会得到想要的结果
public class SomeClass
{
[DataType(DataType.MultilineText)]
public string Name { get; set; }
}
@Html.EditorFor(x => x.Name)
我做了很多努力来找出
TextAreaFor
和Html.EditorFor
之间的区别。你能在网上给出一个简短的回答吗。请?EditorFor将根据其调用的给定属性的属性进行渲染,无论该属性是文本框(字符串)、复选框(bool)等,还是您指定的属性,而TextAreaFor将始终呈现一个文本框。下拉列表也可以由编辑器自动呈现,但复选框将如何自动呈现?我尝试了很多,以找到TextAreaFor
和Html.EditorFor
之间的区别。你能在网上给出一个简短的回答吗。请?EditorFor将根据其调用的给定属性的属性进行渲染,无论该属性是文本框(字符串)、复选框(bool)等,还是您指定的属性,而TextAreaFor将始终呈现文本框。下拉列表也可以由编辑器自动呈现,但如何自动呈现复选框?我需要RouteValueDictionary,因为html属性是作为匿名对象提供的。我没有在这里编写代码,但是扩展需要添加更多属性。@YaronLevi我说的只是你不需要RouteValueDictionary。IDictionary
也可以这样做。我需要RouteValueDictionary,因为html属性是匿名对象。我没有在这里编写代码,但是扩展需要添加更多属性。@YaronLevi我说的只是你不需要RouteValueDictionary。IDictionary
也会这样做。