Does ASP.NET MVC have a way to generate unique ClientIDs?

Is there a ready-made way to create unique "id" tags in ASP.NET MVC?

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

This would be useful when viewing a partial view multiple 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’s easy enough to collapse.

Thank you so much,

John

+3
source share
3 answers

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 as a viewdata object during a call to the renderpartial method to save your ViewUserControl tidy

+5
source

, MVC . ASP.NET MVC . , , Id , . , ?

+1

MSDN:

, Guid .

, , , . Guid , .

.

0

Source: https://habr.com/ru/post/1734269/


All Articles