tags:

views:

76

answers:

2

Hi,

Is there an out-of-the-box way to create unique "id" tags in ASP.NET MVC?

(Similar to the dreaded but sometimes useful ClientIDs in WebForms?)

This would be useful when rendering a partial view many times on a page.

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%-- Example Partial View --%>
<div id="<%=GenerateAUniqueIDHere()%>">
Content
</div>
<script type="text/javascript">
    $("#<%=GenerateAUniqueIDHere%>").hide().fadein().css("font-size", "500%");
</script>

If not, it is easy enough to roll my own.

Thanks much,

Jon

+1  A: 

As far as I know, MVC does not have this. I have used ASP.NET MVC for over a year, and never had to use this. When I have several controls, with the same name, I almost always want to be able to query these controls later, so I need to know the Id, and use a counter, so I know the names. If you don't need to know their ids, why even give them a id?

gautema
A: 

Use a GUID?

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %> 
<%-- Example Partial View --%> 
<%
    string ID = Guid.NewGuid().ToString();
%>
<div id="<%=ID%>"> 
    Content 
</div> 
<script type="text/javascript"> 
    $("#<%=ID%>").hide().fadein().css("font-size", "500%"); 
</script>

I would also pass the GUID in as a viewdata object during the renderpartial method call to keep your ViewUserControl tidy

Mark