asp.net-mvc


How can I make an unordered list appear within a foreach loop in MVC3 Razor?


I have the following code:
#foreach (Content topic in Model.Topics)
{
if (topic.RowKey.Substring(2, 2) == "00")
{
<h3>#topic.RowKey.Substring(0, 2).TrimStart('0') - #Html.Raw(topic.Title)</h3>
}
else
{
<p>#String.Format("{0}.{1}",topic.RowKey.Substring(0, 2).TrimStart('0'),topic.RowKey.Substring(2, 2).TrimStart('0').PadLeft(1, '0')) - #Html.Raw(topic.Title)</p>
}
}
My input data (value of topic.RowKey) looks like this:
0100 <- This is topic 1
0101 <- This is topic 1.1
0102 <- This is topic 1.2
0103 <- This is topic 1.3
0200 <- This is topic 2
0201 <- This is topic 2.1
etc....
This works but what I would really like to do is to have an h3 heading every time the first two digits of the RowKey change and then between then and then next h3 heading I would like to have an unordered list instead of just <p>xxx</p>. I have tried lots of different combinations of things but nothing works. Is this even possible to do with Razor? Where I had huge problems was how can I get the <ul> and </ul>s to appear correctly? I know I can put a <ul> after the <h3> but how can I place the </ul>?
not checked in razor, there may be an # missing here or there, but...
#var groupedTopics = Model.Topics.GroupBy(t => t.RowKey.Substring(0, 2));
#foreach (var group in groupedTopics) {
var firstTopic = group.First();
<h3>#firstTopic.RowKey.Substring(0, 2).TrimStart('0') - #Html.row(firstTopic.Title)</h3>
<ul>
#foreach (var topic in group.Skip(1)) {
<li>#String.Format("{0}.{1}",topic.RowKey.Substring(0, 2).TrimStart('0'),topic.RowKey.Substring(2, 2).TrimStart('0').PadLeft(1, '0')) - #Html.Raw(topic.Title)</li>
}
</ul>
}
It would be more easy create a model parent-child, populated before send it to the view
the model
public class topic {
//everything else
public List<topic> subtopic{ get; set; }
}
the view
#foreach (Content topic in Model.Topics)
{
<h3>#topic.RowKey.Substring(0, 2).TrimStart('0') - #Html.Raw(topic.Title)</h3>
if (topic.subtopic.count > 0)
{
<ul>
#foreach (Content subtopic in topic.subtopic)
{
<li>#String.Format("{0}.{1}", topic.RowKey.Substring(0, 2).TrimStart('0'), topic.RowKey.Substring(2, 2).TrimStart('0').PadLeft(1, '0')) - #Html.Raw(topic.Title)</li>
}
</ul>
}
}
Its not about razor, this is something you can solve with any rendering enginge, web or windows forms or anything else.
#model List<Topic>
#{
int currentKey = 0;
bool withinUL = false;
foreach (Topic topic in Model)
{
if (topic.RowKey != currentKey)
{
currentKey = topic.RowKey;
if (withinUL)
{
withinUL = false;
#:</ul>
}
#:<h3>#topic.RowKey - #Html.Raw(topic.Title)</h3>
}
else
{
if (!withinUL)
{
withinUL = true;
#:<ul></ul>
}
#:<li>#topic.RowKey - #Html.Raw(topic.Title)</li>
}
}
}
#{bool isFirst = true;}
#foreach (Content topic in Model.Topics)
{
if (topic.RowKey.Substring(2, 2) == "00")
{
if(!isFirst) {
<text></ul></text>
isFirst = false
}
<h3>#topic.RowKey.Substring(0, 2).TrimStart('0') - #Html.Raw(topic.Title)</h3>
}
else
{
<li>
<p>#String.Format("{0}.{1}",topic.RowKey.Substring(0, 2).TrimStart('0'),topic.RowKey.Substring(2, 2).TrimStart('0').PadLeft(1, '0')) - #Html.Raw(topic.Title)</p></li>
}
}
</ul>

Related Links

ASP.Net MVC 2 authentication (login) best practices
overload ActionResult
Razor: Creating a view from a nested layout doesn't auto create the sections
Saving entities with foreign key in ASP.NET MVC
“HTTP Error 404.3 - Not Found” force download of .step file in IIS7.5
MVC3 EF4 POCO Repository/UnitOfWork Connection Error
CanvasAutorize - redirection to facebook permissions dialog clears the request_ids query string sent when user accepts an app request
Unable to set membernames from custom validation attribute in MVC2
How do I resolve Dependency Injection in MVC Filter attributes
Extending MVC TemplateHelper / DisplayFor* methods - should I be doing that?
redirect to an external url on form submit
Can we say ASP.NET is also MVC ?
Set class on asp.net input textbox automatically
Static Files not getting routed with IIS the same as they are with Cassini
State Management of javascript conent in MVC
ASP.NET MVC The view 'name' or its master was not found

Categories

HOME
odoo-10
collections
vr
tags
jsessionid
applet
linear-programming
streaming
textbox
wikipedia-api
mvvm-light
c++14
mobilefirst-adapters
copy-paste
stm
pymc3
nanotime
jscrollpane
jquery-selectors
flow
garmin
oracle11gr2
abaqus
powershell-v2.0
spark-graphx
mlr
mediator
google-oauth2
abstract-syntax-tree
parallax
code-signing
fat-free-framework
editorconfig
asp.net-identity-2
buffer-overflow
angular2-databinding
gecko
server-sent-events
macvim
solid-principles
mdanalysis
edeliver
textangular
nscalendar
php-deployer
gretl
log4perl
pseudo-element
ctl
gpt
getelementbyid
setwindowshookex
ng-annotate
kernighan-and-ritchie
cufft
submission
restbed
app-engine-ndb
xlform
document-ready
coldfusion-10
tun
phone-number
rdotnet
telescope
google-swiffy
pencilblue
easynetq
cordova-cli
spiceworks
typesafe-activator
gdk
formhelper
django-postgresql
youtrack-api
sonata-media-bundle
sshj
libtorrent-rasterbar
floating-point-conversion
datapump
tempo
double-precision
square-cube
jmdns
wescheme
timestamp-with-timezone
cellid
j#
facebook-authentication
sharepoint-timer-job
regexbuddy
keylogger
ryz
maemo
fogbugz-on-demand
nhaml
usenet
corporate-policy

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App