Reputation: 27931
ASP .NET MVC2 application.
Site.Master file contains number of ul list link names, url and url content is stored in database table
create table webconte(
elemname char(20) not null,
itemorder integer not null,
caption char(40),
webcontent text,
primay key (elemname, itemorder) )
Number of elements in list and captions are hard coded in view like:
<ul class="nav1">
<li><a href='<%= Url.Action("WebContent", "Home", new { elemname="MainMenu", itemorder=0} ) %>'>caption1</a></li>
<li><a href='<%= Url.Action("WebContent", "Home", new { elemname="MainMenu", itemorder=1} ) %>'>caption2</a></li>
<li><a href='<%= Url.Action("WebContent", "Home", new { elemname="MainMenu", itemorder=2} ) %>'>caption3</a></li>
</ul>
WebContent action returns webcontent field value which contains html page.
How to create view so that:
Number of items is not hard-coded: as many li elements as there are items in database are rendered.
caption1, caption2, ... link texts are retrieved from webconte table caption column
This should be wrapped probalby to partial view to create re-usable component. I'm using MVC2 and C#.
Upvotes: 0
Views: 750
Reputation: 100658
In your controller, collect all your captions into a list of strings and put that list in ViewData. e.g.
var captions = new List<string>();
// add stuff to captions...
ViewData["captions"] = captions
Then on your page you can extract those captions and render your list:
<ul class="nav1">
<%
var captions = ViewData["captions"] as List<string>();
for (int i = 0; i < captions.Count; ++i) {
%>
<li><a href='<%= Url.Action("WebContent", "Home", new { elemname="MainMenu", itemorder=i} ) %>'><%= captions[i] %></a></li>
<% } %>
</ul>
Upvotes: 1