C# 自定义ASP.NET MVC';可渲染';助手,如何添加一些额外的功能?
因此,我正在制作一个自定义html帮助程序,它从传入的C# 自定义ASP.NET MVC';可渲染';助手,如何添加一些额外的功能?,c#,asp.net-mvc,asp.net-mvc-2,html-helper,C#,Asp.net Mvc,Asp.net Mvc 2,Html Helper,因此,我正在制作一个自定义html帮助程序,它从传入的IList中呈现一个表。它工作得很好,但它相当基本。我希望能够指定不从属性名推断的列名。例如,现在我传入一个名为Number的属性,但我想使用№ 我想我已经通过装饰看到了这一点,我只能假设它使用反射来获取值,但我不太确定如何实现它,但 另一件事,我想应用操作单元格属性的能力。例如№单元格的colspan值应为x,或者任何其他单元格都可以应用类。我想那也可能属于某种装饰 最后,我想让一些单元格包含链接,但坦率地说,除了发送一个完整的html字符
IList
中呈现一个表。它工作得很好,但它相当基本。我希望能够指定不从属性名推断的列名。例如,现在我传入一个名为Number的属性,但我想使用№
我想我已经通过装饰看到了这一点,我只能假设它使用反射来获取值,但我不太确定如何实现它,但
另一件事,我想应用操作单元格属性的能力。例如№单元格的colspan
值应为x,或者任何其他单元格都可以应用类。我想那也可能属于某种装饰
最后,我想让一些单元格包含链接,但坦率地说,除了发送一个完整的html字符串作为要呈现的属性值之外,我不知道如何实现它
因此,我在这里发布我当前版本的代码,希望比我更好的人(那就是你,))能给我一些关于如何完成上面所写内容的建议
更新
小更新,目前正在传递的IList是匿名类型,所以我不确定它如何与装饰一起使用
public static class ApplicationExtensions {
public static void RenderTable(
this HtmlHelper HtmlHelper,
IList Items) {
RenderTable(HtmlHelper, string.Empty, Items, null, false);
}
public static void RenderTable(
this HtmlHelper HtmlHelper,
IList Items,
object Attributes) {
RenderTable(HtmlHelper, string.Empty, Items, Attributes, false);
}
public static void RenderTable(
this HtmlHelper HtmlHelper,
IList Items,
IDictionary<string, object> Attributes) {
RenderTable(HtmlHelper, string.Empty, Items, Attributes, false);
}
public static void RenderTable(
this HtmlHelper HtmlHelper,
IList Items,
object Attributes,
bool RenderTFoot) {
RenderTable(HtmlHelper, string.Empty, Items, Attributes.ToDictionary(), RenderTFoot);
}
public static void RenderTable(
this HtmlHelper HtmlHelper,
IList Items,
IDictionary<string, object> Attributes,
bool RenderTFoot) {
RenderTable(HtmlHelper, string.Empty, Items, Attributes, RenderTFoot);
}
public static void RenderTable(
this HtmlHelper HtmlHelper,
string Caption,
IList Items,
object Attributes,
bool RenderTFoot) {
RenderTable(HtmlHelper, Caption, Items, Attributes.ToDictionary(), RenderTFoot);
}
public static void RenderTable(
this HtmlHelper HtmlHelper,
string Caption,
IList Items,
IDictionary<string, object> Attributes,
bool RenderTFoot) {
if ((Items != null) && (Items.Count > 0)) {
StringBuilder TableBuilder = (String.IsNullOrEmpty(Caption) ? new StringBuilder() : new StringBuilder().AppendFormat("<caption>{0}</caption>", Caption));
THead(TableBuilder, Items[0].GetType());
TBody(TableBuilder, Items);
if (RenderTFoot) {
TFoot(TableBuilder, Items[0].GetPropertyCount());
};
TagBuilder TagBuilder = new TagBuilder("table");
TagBuilder.MergeAttributes(Attributes);
TagBuilder.InnerHtml = TableBuilder.ToString();
HtmlHelper.ViewContext.HttpContext.Response.Write(TagBuilder.ToString(TagRenderMode.Normal));
};
}
private static void THead(
StringBuilder TableBuilder,
Type Item) {
TableBuilder.Append("<thead><tr>");
foreach (var Property in Item.GetProperties()) {
TableBuilder.AppendFormat("<th>{0}</th>", Property.Name);
};
TableBuilder.Append("</tr></thead>");
}
private static void TBody(
StringBuilder TableBuilder,
IList Items) {
TableBuilder.Append("<tbody>");
foreach (var Item in Items) {
PropertyInfo[] Properties = Item.GetType().GetProperties();
TableBuilder.Append("<tr>");
foreach (PropertyInfo Property in Properties) {
TableBuilder.AppendFormat("<td>{0}</td>", Property.GetValue(Item, null));
};
TableBuilder.Append("</tr>");
};
TableBuilder.Append("</tbody>");
}
private static void TFoot(
StringBuilder TableBuilder,
int Columns) {
TableBuilder.AppendFormat("<tfoot><tr><td colspan=\"{0}\"><input type=\"button\" value=\"◀\" /><input type=\"button\" value=\"▶\" /></td></tr></tfoot>", Columns);
}
private static int GetPropertyCount(
this object Item) {
return Item.GetType().GetProperties().Count();
}
private static IDictionary<string, object> ToDictionary(
this object Object) {
return Object.GetType().GetProperties().ToDictionary(
k =>
(k.Name),
v =>
(v.GetValue(Object, null)));
}
}
公共静态类ApplicationExtensions{
公共静态void可渲染(
这个HtmlHelper HtmlHelper,
IList项目){
可渲染(HtmlHelper、string.Empty、Items、null、false);
}
公共静态void可渲染(
这个HtmlHelper HtmlHelper,
IList项目,
对象属性){
可渲染(HtmlHelper、string.Empty、Items、Attributes、false);
}
公共静态void可渲染(
这个HtmlHelper HtmlHelper,
IList项目,
(字典属性){
可渲染(HtmlHelper、string.Empty、Items、Attributes、false);
}
公共静态void可渲染(
这个HtmlHelper HtmlHelper,
IList项目,
对象属性,
bool RenderTFoot){
可渲染(HtmlHelper、string.Empty、Items、Attributes.ToDictionary()、RenderFoot);
}
公共静态void可渲染(
这个HtmlHelper HtmlHelper,
IList项目,
IDictionary属性,
bool RenderTFoot){
可渲染(HtmlHelper、string.Empty、Items、Attributes、RenderFoot);
}
公共静态void可渲染(
这个HtmlHelper HtmlHelper,
字符串标题,
IList项目,
对象属性,
bool RenderTFoot){
可渲染(HtmlHelper、标题、项、属性.ToDictionary()、RenderFoot);
}
公共静态void可渲染(
这个HtmlHelper HtmlHelper,
字符串标题,
IList项目,
IDictionary属性,
bool RenderTFoot){
如果((Items!=null)&&(Items.Count>0)){
StringBuilder TableBuilder=(String.IsNullOrEmpty(标题)?new StringBuilder():new StringBuilder().AppendFormat(“{0}”,标题));
THead(TableBuilder,项[0].GetType());
TBody(制表商、项目);
如果(RenderFoot){
TFoot(TableBuilder,项[0].GetPropertyCount());
};
TagBuilder TagBuilder=新的TagBuilder(“表格”);
TagBuilder.MergeAttributes(属性);
TagBuilder.InnerHtml=TableBuilder.ToString();
HtmlHelper.ViewContext.HttpContext.Response.Write(TagBuilder.ToString(TagRenderMode.Normal));
};
}
私有静态无效THead(
StringBuilder和TableBuilder,
类型(项目){
TableBuilder.Append(“”);
foreach(Item.GetProperties()中的var属性){
AppendFormat(“{0}”,Property.Name);
};
TableBuilder.Append(“”);
}
专用静态空盒(
StringBuilder和TableBuilder,
IList项目){
TableBuilder.Append(“”);
foreach(项目中的var项目){
PropertyInfo[]Properties=Item.GetType().GetProperties();
TableBuilder.Append(“”);
foreach(属性中的PropertyInfo属性){
TableBuilder.AppendFormat(“{0}”,Property.GetValue(Item,null));
};
TableBuilder.Append(“”);
};
TableBuilder.Append(“”);
}
私有静态void-TFoot(
StringBuilder和TableBuilder,
int列){
TableBuilder.AppendFormat(“,列);
}
私有静态int GetPropertyCount(
此对象(项目){
返回项.GetType().GetProperties().Count();
}
私有静态IDictionary ToDictionary(
此对象(对象){
返回对象.GetType().GetProperties().ToDictionary(
k=>
(k.姓名),
v=>
(v.GetValue(Object,null));
}
}
它的用法是这样的:
<% Html.RenderTable(Model) // lowest overload... %>
您可能需要从中查看。这应该比编写自己的助手容易得多 “所以我不确定这对装饰有什么影响……”
没有。您将无法从anon获取元数据。嗯,很有趣,但它需要MVC 3。我正在和MVC2打交道,在MVC3完全发布之前,我不打算和它打交道。此外,它似乎试图处理很多Ajax的东西,我不喜欢这些东西,因为它已经限制了我本来要实现的代码设计。我想,我想我的只是一个通用的渲染器,我可以“Ajaxify”,因为我希望。最后,我以前从未构建过HtmlHelper,因此我认为这是一次很好的学习体验。@Alex:MVC 3还为HTML帮助程序提供了一个新概念,即声明性帮助程序。它比代码bas简单得多,而且更易于维护