2009-03-25 24 views
5

benim htmlhelper uzatma yöntemi için benzer yapıdaki vektörlerin içine klonlanabilir desteklemek istiyorlarsa bir htmlhelper uzantısı yazma, ben şöyle RouteValueDictionary kullanın. .. htmlAttributes'u 'a atayamayacağınızı biliyorum ... ... ama neden ve neden şaşkın olduğumdan emin değilim. RouteValueDictionary Yönlendirme ile yapılmamalıdır ve bu nedenle HtmlHelper yöntemleri ile ilgisi yoktur? Dediğim gibi, muhtemelen noktayı kaçırıyorum, o yüzden birisinin bana neyi özlediğimi söyleyebilseydim sevinirim.HtmlHelper yöntemleri ve RouteValueDictionary

Şerefe ... düzenleme

: Dan cevap cevaben ->

Sadece giriş yardımcıları için mvc kaynak kodunda kullanımda gördüklerini takip ediyordu ...

  • "src\SystemWebMvc\Mvc\Html\InputExtensions.cs"

o tarihte aşağıdaki yok gibi bakın:

public static string TextBox(this HtmlHelper htmlHelper, 
          string name, 
          object value, 
          object htmlAttributes) 
{ 
    return TextBox(htmlHelper, 
        name, 
        value, 
        new RouteValueDictionary(htmlAttributes)) 
} 

Açıkçası bir kısayol, ama bu bir piç mi yoksa tamam mı?

cevap

5

Rob Conery'nin blog post modelini buna benzer bir şeyle izlemenizi önemle tavsiye ederim.

Codedump:

et ve bunun sebze şudur

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; 
} 

Kullanımı:

public static string ListBoxDict(this HtmlHelper htmlHelper, 
           string name, 
           object value, 
           object htmlAttributes) 
{ 
    return htmlHelper.ListBoxDict(name, 
            value, 
            htmlAttributes.ToAttributeList())); 
} 

için htmlAttribute nesneyi dönüştürmek Ne .ToAttributeList() olduğunu mu

name = "değer"

Umut bu mantıklı.

+0

Teşekkürler Dan, ilginç görünüyor ve ben bir göz atacağım. Sorumu düzenledim çünkü mvc framework yardımcıları –

+2

Huh'da zaten olanı takip ettiğimi söylemedim. Bunu başka bir proje için tekrar arıyordum ve kendi cevabımı buldum! Ne şans! –

+0

+1 Teşekkürler, bir projede bana yardımcı oldu ... – xandercoded

İlgili konular