Skip to content
14/07/2008 / dimarzionist

Control’s identity issues in MVC

This part of code:

<% foreach (Review r in ViewData.PreviousReviews)

{%>

<li><%=Html.RenderUserControl(“~/Views/MyContribution/PreviousReview.ascx”, r)%></li>

<%}%>

Generates multiple controls with SAME id property. It’s wrong in my opinion. So when you trying to access them from client script or server side code by getElementById() or FindControl() you will always hae access only to first one, because all other controls have the same identity!

So I need to use some additional syntax to fight this:

<% foreach (Review r in ViewData.PreviousReviews)

{%>

<li><%=Html.RenderUserControl(“~/Views/MyContribution/PreviousReview.ascx”, r, new {ID = “cr” + r.ReviewId})%></li>

<%}%>

In contrast in standard ASP.NET when we generates multiple controls (in repeater for example) without identities, they automatically receive different auto-generated id.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: