ID generation for HTML elements in ASP.NET MVC

后端 未结 4 884
刺人心
刺人心 2021-02-19 08:08

I need to generate unique identifiers for html elements in asp.net mvc application. In classic asp.net i could use

%>


        
相关标签:
4条回答
  • 2021-02-19 08:19

    There is no single solution to this.

    You need to modify your code to generate IDs based on whatever is generating the elements.

    For example, if you're looping over rows from a database, you can use the rows' primary keys to generate IDs.

    Alternatively, you can eschew IDs altogether and use non-unique classes. (this is especially convenient with jQuery and descendant selectors)

    0 讨论(0)
  • 2021-02-19 08:23

    May be this draft code help you:

    static class MyIdGenerator
    {
        public static int NextID()
        {
            static int _id = 0;
            return _id++;
        }
    }
    

    With static counter every call NextID() will return next Id;

    0 讨论(0)
  • 2021-02-19 08:28

    I liked the answer you provided in your Update better than using a Guid, because the latter will be different each time which makes client-side debugging and finding an element in View Source more difficult.

    I took it a step further and added a custom prefix.. each prefix uses its own counter to help even further in that regard.

        public static string GetUniqueHtmlid(this HtmlHelper html, string prefix)
        {
            var generator = html.ViewContext.HttpContext.Items[typeof (UniqueHtmlIdGenerator)] as UniqueHtmlIdGenerator;
    
            if(generator == null)
                html.ViewContext.HttpContext.Items[typeof(UniqueHtmlIdGenerator)] = generator = new UniqueHtmlIdGenerator();
    
            return generator.GetNextUniqueId(prefix);
        }
    
        private class UniqueHtmlIdGenerator
        {
            private readonly Dictionary<string, int> _items = new Dictionary<string, int>();
    
            public string GetNextUniqueId(string prefix)
            {
                if (string.IsNullOrEmpty(prefix))
                    prefix = "item";
    
                int current;
    
                lock (typeof (UniqueHtmlIdGenerator))
                {
                    current = _items.ContainsKey(prefix) ? _items[prefix] : 1;
    
                    _items[prefix] = current + 1;
                }
    
                return string.Format("{0}-{1}", prefix, current);
            }
        }
    
    0 讨论(0)
  • 2021-02-19 08:36

    Simplest correct solution using built-in .NET libraries with no new custom application code required

    Use Guid.NewGuid(), with the ToString() numeric representation "N" in order to prevent invalid characters that could browser JS issues.

    Guid.NewGuid().ToString("N");
    

    Quoting MSDN regarding the "N" format specifier:

    32 digits: 00000000000000000000000000000000

    Don't use the default GUID representation as hyphens can be problematic to work with in JS/jQuery.

    For completeness, it's best prepend a letter to the beginning of the GUID. Although I've never experienced issues with this in modern browsers, technically an HTML id has to begin with a letter and not a number.

    0 讨论(0)
提交回复
热议问题