मुझे data-myid
स्वीकार करने के लिए अज्ञात प्रकार की घोषणा प्राप्त करने का कोई तरीका नहीं दिख रहा है, क्योंकि यह C# में मान्य संपत्ति नाम नहीं है। एक विकल्प के लिए एक नया अधिभार कि एक अतिरिक्त dataAttributes
पैरामीटर लेता है, और आप के लिए नामों को data-
पहले जोड़ता बनाने के लिए किया जाएगा ...
using System.ComponentModel;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.Routing;
static class TextBoxExtensions
{
public static string TextBox(this HtmlHelper htmlHelper, string name, object value, object htmlAttributes, object dataAttributes)
{
RouteValueDictionary attributes = new RouteValueDictionary(htmlAttributes);
attributes.AddDataAttributes(dataAttributes);
return htmlHelper.TextBox(
name,
value,
((IDictionary<string, object>)attributes);
}
private static void AddDataAttributes(this RouteValueDictionary dictionary, object values)
{
if (values != null)
{
foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(values))
{
object obj2 = descriptor.GetValue(values);
dictionary.Add("data-" + descriptor.Name, obj2);
}
}
}
}
तो फिर तुम
<%= Html.TextBox ("textBox", "Value",
new { title = "Some ordinary attribute" },
new { myid = m.ID }) %>
हालांकि साथ एक data-myid
विशेषता जोड़ सकते हैं , जो आपको उन अन्य विधियों पर अधिभार बनाने के लिए छोड़ देता है जिन्हें आप डेटा विशेषताओं को स्वीकार करना चाहते हैं, जो दर्द है। आपको लगता है कि चारों ओर एक
public static IDictionary<string,object> MergeDataAttributes(
this HtmlHelper htmlHelper,
object htmlAttributes,
object dataAttributes)
के तर्क को ले जाकर मिलता है और के रूप में
<%= Html.TextBox ("textBox", "Value",
Html.MergeDataAttributes(new { title = "Some ordinary attribute" }, new { myid = m.ID })) %>
इस प्रश्न को 'एस्पनेट एमवीसी के साथ हाइफेनेटेड एचटीएमएल विशेषताओं' देखें http://stackoverflow.com/questions/2897733/hyphenated-html-attributes-with-asp-net-mvc/12091886#12091886 –
नोट: नया सही उत्तर http: // stackoverflow है।कॉम/ए/5872576/1037 9 48, यदि आपको स्क्रॉलिंग पसंद नहीं है तो इस पृष्ठ को आगे बढ़ाएं;) – drzaus