htmlhelper 방법 및 RourEvaluedictionary
-
22-08-2019 - |
문제
htmlhelper 확장 방법을 위해 유사하게 구조화 된 CTORS를 지원하려면 HTMLHELPER 확장을 작성할 때 사용합니다. RouteValueDictionary
다음과 같이 :
public static string ListBoxDict(this HtmlHelper htmlHelper,
string name,
object value,
object htmlAttributes)
{
return ListBoxDict(htmlHelper,
name,
value,
((IDictionary<string, object>)
new RouteValueDictionary(htmlAttributes)));
}
내 질문은 정말로 필요한 이유입니다 RouteValueDictionary
... 나는 당신이 그냥 캐스팅 할 수 없다는 것을 알고 있습니다 htmlAttributes
에게 IDictionary<string, object>
... 왜 그런지 잘 모르겠지만 그것이 내가 혼란 스러울 수도 있습니다. 그렇지 않아야합니다 RouteValueDictionary
라우팅과 관련이 있으므로 htmlhelper 방법과 관련이 없습니까? 내가 말했듯이, 나는 아마도 요점을 놓치고있을 것입니다. 누군가 내가 놓친 것을 말해 줄 수 있다면 기뻐할 것입니다.
건배...
편집 : Dan의 답변에 대한 응답 ->
입력 도우미를위한 MVC 소스 코드에서 사용한 것을 따르고있었습니다.
- 보다 "
src\SystemWebMvc\Mvc\Html\InputExtensions.cs
"
다음과 같이합니다.
public static string TextBox(this HtmlHelper htmlHelper,
string name,
object value,
object htmlAttributes)
{
return TextBox(htmlHelper,
name,
value,
new RouteValueDictionary(htmlAttributes))
}
분명히 바로 가기이지만 녀석입니까, 아니면 그렇게해도 괜찮습니까?
해결책
나는 Rob Conery를 보는 것을 강력히 추천합니다 블로그 게시물 이와 같은 것에 대해.
그것의 고기와 채소는 이것입니다.
CodeDump :
public static string ToAttributeList(this object list)
{
StringBuilder sb = new StringBuilder();
if (list != null)
{
Hashtable attributeHash = GetPropertyHash(list);
string resultFormat = "{0}=\"{1}\" ";
foreach (string attribute in attributeHash.Keys)
{
sb.AppendFormat(resultFormat, attribute.Replace("_", ""),
attributeHash[attribute]);
}
}
return sb.ToString();
}
public static string ToAttributeList(this object list,
params object[] ignoreList)
{
Hashtable attributeHash = GetPropertyHash(list);
string resultFormat = "{0}=\"{1}\" ";
StringBuilder sb = new StringBuilder();
foreach (string attribute in attributeHash.Keys)
{
if (!ignoreList.Contains(attribute))
{
sb.AppendFormat(resultFormat, attribute,
attributeHash[attribute]);
}
}
return sb.ToString();
}
public static Hashtable GetPropertyHash(object properties)
{
Hashtable values = null;
if (properties != null)
{
values = new Hashtable();
PropertyDescriptorCollection props =
TypeDescriptor.GetProperties(properties);
foreach (PropertyDescriptor prop in props)
{
values.Add(prop.Name, prop.GetValue(properties));
}
}
return values;
}
용법:
public static string ListBoxDict(this HtmlHelper htmlHelper,
string name,
object value,
object htmlAttributes)
{
return htmlHelper.ListBoxDict(name,
value,
htmlAttributes.ToAttributeList()));
}
뭐 .ToAttributeList()
htmlattribute 객체를 변환하는 것입니다
이름 = "값"
이것이 말이되기를 바랍니다.
제휴하지 않습니다 StackOverflow