Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 设置显示器的格式_C#_Asp.net Mvc 3_Razor - Fatal编程技术网

C# 设置显示器的格式

C# 设置显示器的格式,c#,asp.net-mvc-3,razor,C#,Asp.net Mvc 3,Razor,我有一个列表,作为模型的一部分返回。我希望列表中的每个项目都显示在自己的行中。我有两种方法,这两种方法都不管用 一种方法是使用foreach(),这将允许我在每个项中使用break或其他形式。但要做到这一点,我必须使模型IEnumerable(或者至少我认为这是我必须做的),我不想这样做 但是,如果我将其设置为常规模型,则foreach()将不起作用。有人能给我一个解决方案吗 这就是我所说的“常规模式”(我是一个不懂正确术语的人) 下面是模型本身的列表 public List<string

我有一个列表,作为模型的一部分返回。我希望列表中的每个项目都显示在自己的行中。我有两种方法,这两种方法都不管用

一种方法是使用foreach(),这将允许我在每个项中使用break或其他形式。但要做到这一点,我必须使模型IEnumerable(或者至少我认为这是我必须做的),我不想这样做

但是,如果我将其设置为常规模型,则foreach()将不起作用。有人能给我一个解决方案吗

这就是我所说的“常规模式”(我是一个不懂正确术语的人)

下面是模型本身的列表

public List<string> ReferralTypeNames { get; set; }
public List ReferralTypeNames{get;set;}
下面是视图中的线条:

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

这是可行的,但项目之间没有换行符

更新 以下是我想要的示例:
学术部
学术增强教程
入学人数
另一位学术顾问

这是我得到的一个样本: 学术部学术增强教程招生其他学术顾问

以下是表单其余部分的代码:

<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>

学生证
@ViewBag.CurrentStudentId
用户名
@ViewBag.CurrentStudentUsername
名称
@ViewBag.LName
指派顾问
@DisplayFor(model=>model.AdvisorName)
宣布少校
@DisplayFor(model=>model.Major)
查看不同的建议会话
@DisplayFor(model=>model.AppointmentDates)
顾问
(进行任命的人员)
(名称)@Html.DisplayFor(model=>model.AdvisorId) 联系人/约会类型 @DisplayFor(model=>model.ContactTypeName) 预约单 @DisplayFor(model=>model.Notes) 建议话题 @DisplayFor(model=>model.AdvisingTopicNames) 转介主题 @Html.DisplayFor(model=>model.ReferralTypeNames)
试试看

@foreach(Model.ReferralTypeNames中的字符串s)
{
@Html.DisplayFor(m=>s)
}
试试看

@foreach(Model.ReferralTypeNames中的字符串s)
{
@Html.DisplayFor(m=>s)
}
如果要自定义自定义类型的显示(或编辑),可以使用MVC显示/编辑器模板。这是一种优雅的方式。@gustavodidomenico如果所说的类型是
列表
,似乎有些过分了……我几乎同意你的观点@millimoose,因为我更喜欢这种过分的方式,而不是在主视图中循环和自定义内容。我很确定他想要特征线,也许还有其他格式…@JustinPihony不是重复的。我已经看过了。它是关于在单个项目中强制换行。我正在尝试在项目之间插入分隔符。的可能重复如果要自定义自定义类型的显示(或编辑),可以使用MVC显示/编辑器模板。这是一种优雅的方式。@gustavodidomenico如果所说的类型是
列表
,似乎有些过分了……我几乎同意你的观点@millimoose,因为我更喜欢这种过分的方式,而不是在主视图中循环和自定义内容。我很确定他想要特征线,也许还有其他格式…@JustinPihony不是重复的。我已经看过了。它是关于在单个项目中强制换行。我正在尝试在项目之间插入一个分隔符。鉴于它是一个字符串,不妨将循环体
@s
,不是吗?(除非你真的想定制普通字符串的呈现方式。)这是可行的。就我个人而言,我更喜欢显示,因为从视图模型渲染时,它更一致,将来更容易更改数据类型。我尝试了
,这是我尝试的第一件事。没有效果。如果我理解这里发生了什么,我可以在DisplayFor周围包装一个html标记,但我不能(仅从视图)影响它内部发生的事情;也就是说,在每次迭代中。我认为DisplayFor做了它所做的事情,并立即返回整个字符串。抱歉,但我认为您没有注意到我说过的,当我选择foreach时,我收到一条消息,即模型必须是IEnumerable。如果我做了这个模型,它会破坏很多其他东西。我想我可以解决所有这些问题,但我特别想问的是如何在不丢失模型类型的情况下使用换行符。
List
实现了
IEnumerable
,因此它可以在
foreach
中使用。您能提供导致错误消息的代码吗?既然它是一个字符串,那么最好使循环体
@s
,否?(除非你真的想定制普通字符串的呈现方式。)这是可行的。就我个人而言,我更喜欢显示,因为从视图模型渲染时,它更一致,将来更容易更改数据类型。我尝试了
,这是我尝试的第一件事。没有效果。如果我理解这里发生了什么,我可以在DisplayFor周围包装一个html标记,但我不能(仅从视图)影响它内部发生的事情;也就是说,在每次迭代中。我认为DisplayFor做了它所做的事情,并立即返回整个字符串。抱歉,但我认为您没有注意到我说过的,当我选择foreach时,我收到一条消息,即模型必须是IEnumerable。如果我做了这个模型,它会破坏很多其他东西。我想我可以解决所有这些问题,但我特别想问的是如何在不丢失模型类型的情况下使用换行符。
List
实现了
IEnumerable
,因此它可以在
foreach
中使用。您能否提供导致错误消息的代码?
<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>
@foreach (string s in Model.ReferralTypeNames)
{
    @Html.DisplayFor(m => s)<br />
}