what is the equivalent of asp:datalist in asp.net-mvc
-
12-09-2019 - |
Question
i am migrating a site over to asp.net from an asp.net site and one of the pages has a datalist as below:
<asp:DataList ID="MyDataList" runat="server" BackColor="#EEEEEE" CellPadding="10"
ItemStyle-HorizontalAlign="Center" GridLines="Both" Width="750" RepeatDirection="Horizontal"
RepeatColumns="4" RepeatLayout="Table" ItemStyle-VerticalAlign="Top">
<ItemTemplate>
<table>
<tr align="center">
<td valign="top">
<table>
<tr>
<td width="30%">
</td>
<td>
<asp:HyperLink ID="HyperLink1" runat="server">
<asp:Image style="cursor:pointer" CssClass="instant ishadow50" ID="lnkEnlarge" runat="server"></asp:Image></asp:HyperLink>
</td>
<td width="30%">
</td>
</tr>
</table>
</td>
</tr>
<tr align="center">
<td>
<asp:Label CssClass="Comments" ID="lblComment" runat="server"></asp:Label><br>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
what is the equivalent in asp.net-mvc. how would i go about migrating?
Solution
ASP.NET MVC has no server controls. you can use a simple foreach loop on your model (you can use a partial view too).
different option is to write a Html helper.
OTHER TIPS
@{
//repeatdirection = Horizontal, RepeatColumns = 4
const int NumberOfColumns = 4;
int skip = 0;
var items = Model.DataStuff.Skip(skip).Take(NumberOfColumns);
while(items.Count() > 0) {
<tr>
@foreach (var item in items) {
<td>
@Html.ActionLink(item.Name...etc)
</td>
}
</tr>
skip += NumberOfColumns;
items = Model.Skip(skip).Take(NumberOfColumns);
}
}
As CD points out, the basic way is to just write the Html. Take it back to the old school. There are some more advanced helpers avaliable if you prefer to take a more abstracted approach. One good option for stuff like this is the MvcContrib grid -- it will generally keep you out of direct html generation while still working MVC style.
NB: link points to a version of the MvcContrib grid that post-dates the public release on CodePlex at the time of this writing, you will need to grab the source and build your own to take advantage of it.