1

I have a list that I return as part of a model. I want to be have each item in the list to appear on its own line. I have two approaches, neither of which quite do the job.

One way is to use a foreach(), which would allow me to use a break or whatever in each item. But to do that I have to make the model IEnumerable (or at least I think that's what I have to do), and I don't want to do that.

If I make it a regular model, though, a foreach() doesn't work. Can anyone guide me to a solution?

This is what I mean by a "regular model" (I'm too much of a noob to know the correct term)

@model AcademicAdvising.Models.AppointmentModel

Here's the list from the model itself

public List<string> ReferralTypeNames { get; set; }

And here's the line as it stands right now in the view:

<p>@Html.DisplayFor(model => model.ReferralTypeNames)</p>

That works, but there are no line breaks between items.

UPDATE Here's a sample of what I want:
Academic Department
Academic Enhancement-Tutorial
Admissions
Another Academic Advisor

And here's a sample of what I get: Academic DepartmentAcademic Enhancement-TutorialAdmissionsAnother Academic Advisor

Here is the code for the rest of the form:

<table class="table-appt">
    <tr>
        <th>Student ID</th>
        <td>@ViewBag.CurrentStudentId</td>
    </tr>
    <tr>
        <th>Username</th>
        <td>@ViewBag.CurrentStudentUsername</td>
    </tr>
    <tr>
        <th>Name</th>
        <td>@ViewBag.LName</td>
    </tr>
    <tr>
        <th>Assigned Advisor</th>
        <td>@Html.DisplayFor(model => model.AdvisorName)</td>
    </tr>
    <tr>
        <th>Declared Major</th>
        <td>@Html.DisplayFor(model => model.Major)</td>
    </tr>
</table>

<fieldset>
    <legend></legend>

    <div class="field">
        <h4>View a Different Advising Session</h4>
        <div>
            @Html.DisplayFor(model => model.AppointmentDates)
        </div>
    </div>

    <div class="field">
        <h3>Advisor</h3>
        (person conducting appointment)<br />
        <div>(name) @Html.DisplayFor(model => model.AdvisorId)</div>
    </div>

    <div class="field">
        <h3>Contact/Appointment Type</h3>
        <div>@Html.DisplayFor(model => model.ContactTypeName)</div>
    </div>

    <div class="field">
        <h3>Appointment Notes</h3>
        <div class="display-field">
            @Html.DisplayFor(model => model.Notes)
        </div>
    </div>

    <div class="field">
        <h3>Advising Topics</h3>
        <div>@Html.DisplayFor(model => model.AdvisingTopicNames)</div>
    </div>

    <div class="field">
        <h3>Referral Topics</h3>
        @Html.DisplayFor(model => model.ReferralTypeNames)<br />
    </div>

</fieldset>
jrummell
  • 42,637
  • 17
  • 112
  • 171
Skip Knox
  • 147
  • 1
  • 1
  • 7
  • possible duplicate of [How to make Html.DisplayFor display line breaks?](http://stackoverflow.com/questions/9030763/how-to-make-html-displayfor-display-line-breaks) – Justin Pihony Mar 20 '13 at 16:14
  • If want to customize the display (or edit) of a custom type you could use the MVC Display/Editor templates. It is an elegant way. – gustavodidomenico Mar 20 '13 at 16:20
  • @gustavodidomenico Seems overkill if said type is a `List`... – millimoose Mar 20 '13 at 16:22
  • I almost agree with you @millimoose, because I do prefer this overkill in place of looping and customizing things inside the main view. I am quite sure that he wants breaklines, and maybe other formatting... – gustavodidomenico Mar 20 '13 at 16:25
  • @JustinPihony Not a duplicate. I already looked at that one. It's about forcing linebreaks inside a single item. I'm trying to insert a break *between* items. – Skip Knox Mar 20 '13 at 17:04

1 Answers1

1

try

@foreach (string s in Model.ReferralTypeNames)
{
    @Html.DisplayFor(m => s)<br />
}
polybios
  • 1,159
  • 8
  • 20
  • Seeing as it's a string, might as well make the loop body `@s`, no? (Unless you really really want to customise rendering of plain strings.) – millimoose Mar 20 '13 at 16:22
  • It would work. Personally I prefer DisplayFor when rendering from view model, it is more consistent and easier to change data type in future. – polybios Mar 20 '13 at 16:31
  • I tried the
    , it was one of the first things I tried. No effect. If I am understanding what's going on here, I can wrap an html tag *around* a DisplayFor, but I can't (just from the View) affect what happens *inside* it; that is, within each iteration. I think DisplayFor does what it does and returns the entire string at once.
    – Skip Knox Mar 20 '13 at 17:03
  • Sorry, but I think you missed where I said that when I pick foreach I get a message that the model must be IEnumerable. If I make the model that, it breaks a bunch of other stuff. I suppose I can fix all that, but I was specifically asking how to have the linebreaks without losing the model type. – Skip Knox Mar 20 '13 at 19:24
  • 1
    `List` implements `IEnumerable` so it can be used in `foreach`. Can you provide the code which causes the error message? – polybios Mar 20 '13 at 19:32
  • I can't post code in a reply and have it format properly. How do I post it formatted? The only place I can find is to add another update to the original post. – Skip Knox Mar 20 '13 at 20:00
  • Augh. When someone gives you advice, read carefully! I was getting the error because I had (string x in Model) I saw the red line come up and immediately retreated. I took a coworker to noticed that I needed (string x in Model.ReferralTypeNames) He said that string already has a built-in editor helper, so it knows what to do. Thanks for sticking with my, polybios. Much appreciated. – Skip Knox Mar 20 '13 at 21:23