English 中文(简体)
MVC3模式中的习俗超文本属性
原标题:Custom HTML attributes in MVC3 Model

我想创建一种模式:

public class TestModel
{
    Microdata(Data = "data-test=this is a test!")]
    public bool Test { get; set; }
}

接着认为:

@Html.DisplayForModel()

期望的结果就是:

<label>Test:</label> <input type="checkbox" data-test="this is a test!" />

我已经创造了一种习俗属性,但这种说法没有产生任何结果。

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
public class MicrodataAttribute : Attribute
{
    public string Data { get; set; }

    public RouteValueDictionary GetAttributes()
    {
        var attributes = new RouteValueDictionary();

        if (this.Data != null)
        {
            string[] kv = this.Data.Split( , );
            attributes.Add(kv[0], kv[1]);
        }
        return attributes;
    }
}



public class MetadataProvider : DataAnnotationsModelMetadataProvider
{
    protected override ModelMetadata CreateMetadata(IEnumerable<Attribute> attributes, Type containerType, Func<object> modelAccessor, Type modelType, string propertyName)
    {
        var metadata = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName);
        var additionalValues = attributes.OfType<HtmlPropertiesAttribute>().FirstOrDefault();
        if (additionalValues != null)
        {
            metadata.AdditionalValues.Add("HtmlAttributes", additionalValues);
        }
        return metadata;
    }
}
最佳回答

为什么? 没有使用你的属性的法律......

阅读下文的博客员额——介绍MVC如何使用元数据,并举例说明一个习俗<代码>。 您需要撰写模板:

问题回答

After discussing the issue with @JakubKonecki and reading the blog post he presented. Here is my EditTemplate used to help create data dash attributes in MVC 2/3 and most likely 4.

I saved this file under root/Views/Shared/EditTemplates as String.cshtml. cshtml since i m using the razor engine. The location can be different if you are using Area s and they don t have to be stored in "Shared" views folder. Just read the entire blog @JakubKonecki posted by Brad Wilson.

再次感谢 @JakubKonecki!

@{
    Dictionary<string, object> AV = ViewData.ModelMetadata.AdditionalValues;
    Dictionary<string, object> htmlAttr = new Dictionary<string,object>();
    foreach (KeyValuePair<string, object> A in AV)
    {
        if (A.Value is System.Web.Routing.RouteValueDictionary)
        {
            foreach (KeyValuePair<string, object> B in (System.Web.Routing.RouteValueDictionary)A.Value)
            {
                htmlAttr.Add(B.Key, B.Value);
            }
        }
    }
    htmlAttr.Add("class", "text-box single-line");
    htmlAttr.Add("type", "text");
}

@Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue, htmlAttr)




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签