나는 당신이 당신의 목표를 달성하기 위해 expandos 처리 할 필요가 있다고 생각하지 않습니다
public static class HtmlExtensions
{
public static IHtmlString MyHelper(this HtmlHelper htmlHelper, object htmlAttributes)
{
var builder = new TagBuilder("div");
// define the custom attributes. Of course this dictionary
// could be dynamically built at runtime instead of statically
// initialized as in my example:
builder.MergeAttribute("data-myattribute1", "value1");
builder.MergeAttribute("data-myattribute2", "value2");
// now merge them with the user attributes
// (pass "true" if you want to overwrite existing attributes):
builder.MergeAttributes(new RouteValueDictionary(htmlAttributes), false);
builder.SetInnerText("hello world");
return new HtmlString(builder.ToString());
}
}
및 기존 헬퍼의 일부를 호출하기를 원한다면, 다음 간단한 foreach 루프가 일을 할 수 있습니다 :
public static class HtmlExtensions
{
public static IHtmlString MyHelper(this HtmlHelper htmlHelper, object htmlAttributes)
{
// define the custom attributes. Of course this dictionary
// could be dynamically built at runtime instead of statically
// initialized as in my example:
var myAttributes = new Dictionary<string, object>
{
{ "data-myattribute1", "value1" },
{ "data-myattribute2", "value2" }
};
var attributes = new RouteValueDictionary(htmlAttributes);
// now merge them with the user attributes
foreach (var item in attributes)
{
// remove this test if you want to overwrite existing keys
if (!myAttributes.ContainsKey(item.Key))
{
myAttributes[item.Key] = item.Value;
}
}
return htmlHelper.ActionLink("click me", "someaction", null, myAttributes);
}
}
도우미 코드를 표시 할 수 있습니까? 그것은 당신의 목표를 잘 설명 할 것입니다. 나는 당신이 어떤 ExpandoObject도 필요로하지 않으며 당신의 목표를 성취 할 다른 방법이있을 것이라고 생각합니다. –
@DarinDimitrov : 예, 맞습니다 :) Dictionary의 문제를 해결했습니다. [ExpandoObject를 익명 형식으로 캐스트] 가능한 –
xx77aBs
복제본 (http://stackoverflow.com/questions/10241776/cast- expandoobject-to-anonymous-type) – nawfal