【问题标题】:How do you use a list of strings within a Razor view?如何在 Razor 视图中使用字符串列表?
【发布时间】:2011-11-18 14:27:29
【问题描述】:

我正在将一个字符串列表传递到一个 razor 视图页面,我想在呈现 HTML 要求更正它们之前检查是否有任何错误。

我的代码可以正常编译,但在呈现页面时会产生编译错误。 错误是:

说明:编译资源时出错 需要为该请求提供服务。请查看以下具体内容 错误详细信息并适当地修改您的源代码。

编译器错误消息: CS1501:方法 'Write' 没有重载需要 0 论据

这是我要渲染的代码:

@model UNICH.Settings.SettingsModel
@{
    ViewBag.Title = "Edit";
}
@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)


    @{ 
         // Retrieve the server generated config errors from the ViewData
         List<string> errors = ViewData["ConfigErrors"] as List<string>;

         if( errors != null && errors.Count > 0 )
         {
             // We have some errors to display 
            <div>
                <h3>The following errors need attention before the configuration can be saved</h3>
                <ul>
                    @foreach( var error in errors)
                    {
                        // Display the errors that require attention
                        <li>
                            @error
                        </li>
                    }
                </ul>
            </div>
        }
     }    

    <fieldset>
        <legend>Database Configuration</legend>
        <table>
            <tr>
                <th>
                    Parameter
                </th>
                <th>
                    Value
                </th>
                <th>
                    Error
                </th>
            </tr>
            <tr>
                <td>
                    <h4>
                        Database Type</h4>
                </td>
                <td>
                    @Html.DropDownListFor(model => model.DBType, ViewData["DBTypes"] as SelectList, "select a value")
                </td>
                <td>
                    @Html.ValidationMessageFor(model => model.DBType)
                </td>
            </tr>
            <tr>
                <td>
                    <h4>
                        Server Name</h4>
                </td>
                <td>
                    @if( Model.DBType != "Oracle")
                {
                        @Html.EditorFor(model => model.DBServerName)
                }
                </td>
                <td>
                    @Html.ValidationMessageFor(model => model.DBServerName)
                </td>
            </tr>
            <tr>
                <td>
                    <h4>
                        Instance Name</h4>
                </td>
                <td>
                    @Html.EditorFor(model => model.DBInstanceName)
                </td>
                <td>
                    @Html.ValidationMessageFor(model => model.DBInstanceName)
                </td>
            </tr>
            <tr>
                <td>
                    <h4>
                        DB User Name</h4>
                </td>
                <td>
                    @Html.EditorFor(model => model.DBUsername)
                </td>
                <td>
                    @Html.ValidationMessageFor(model => model.DBUsername)
                </td>
            </tr>
            <tr>
                <td>
                    <h4>
                        Database Password</h4>
                </td>
                <td>
                    @Html.EditorFor(model => model.DBPassword)
                </td>
                <td>
                    @Html.ValidationMessageFor(model => model.DBPassword)
                </td>
            </tr>
        </table>
    </fieldset>
    <div>
        <p>
            <input type="submit" value="Save" />
        </p>
    </div>
}
<div>
    @Html.ActionLink("Discard Changes", "Index")
</div>

【问题讨论】:

  • 只是想知道,为什么要在字符串上调用 ToString()?
  • @RichardD,这只是一种沮丧的行为,并尝试任何事情让它发挥作用。 :o(
  • 很公平:D 我有很多类似的事情。
  • 对我来说仍然可以正常工作。这就是你所有的代码吗?它是否告诉您错误发生在哪一行?
  • @TeamWild 这很有意义,您可以将其保留在 BeginForm 块内,但只需删除它周围的 @{ }。您应该发布您的解决方案作为答案并接受它。

标签: asp.net-mvc-3 razor


【解决方案1】:

您不需要将 ViewData 值转换为 List&lt;string&gt; 的服务器端代码周围的 @{}。附带说明一下,如果您使用视图模型而不是 ViewData 弱类型结构,则不需要强制转换。当然,如果您使用了强类型,则无需将其包装在 @{ ... } 中,也不会出现任何错误。结论:永远不要在 ASP.NET MVC 应用程序中使用ViewBag/ViewData。始终使用视图模型和强类型视图。

这是正确的代码斜体是正确的,因为它可以工作,但根据我之前的旁注不推荐):

@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)

    // Retrieve the server generated config errors from the ViewData
    List<string> errors = ViewData["ConfigErrors"] as List<string>;

    if( errors != null && errors.Count > 0 )
    {
        // We have some errors to display 
        <div>
            <h3>The following errors need attention before the configuration can be saved</h3>
            <ul>
                @foreach( var error in errors)
                {
                    // Display the errors that require attention
                    <li>
                        @error
                    </li>
                }
            </ul>
        </div>        
    }

    <fieldset>
        ...
    </fieldset>

    <div>
        <p>
            <input type="submit" value="Save" />
        </p>
    </div>
}

<div>
    @Html.ActionLink("Discard Changes", "Index")
</div>

【讨论】:

  • 现在我很困惑...您的回答与 RichardD 的建议相同,但您关于不使用 ViewBag/ViewData 的评论与我一直在研究的所有教程背道而驰。这并不意味着你错了......我会试一试的。
  • @TeamWild,教程的问题在于它们倾向于将事情过度简化,并且不幸地教给人们不良做法,例如 ViewBag/ViewData,它们会在您的视图中引入弱输入并将它们转换为意大利面条代码,您将在其中在 C# 等强类型语言中对抗这种弱类型。这就是视图模型的设计目的:反映视图的要求。因此,如果在您的视图中,您需要显示一个错误列表,那么这个错误列表应该是您的视图模型的一个属性,并且您应该使用 SettingsViewModel 而不是 SettingsModel
  • 感谢您的解释。这很有道理。我将 sting 列表移到了我的模型中,它使控制器、视图甚至填充模型的服务中的代码看起来更清晰。
【解决方案2】:

问题与代码块的位置有关。将代码块移到 Html.BeginForm() 括号外解决了这个问题。

    @{ 
    // Retrieve the server generated config errors from the ViewData
    List<string> errors = ViewData["ConfigErrors"] as List<string>;

    if( errors != null && errors.Count > 0 )
    {
        // We have some errors to display 
        <div>
            <h3>The following errors need attention before the configuration can be saved</h3>
            <ul>
                @foreach( var error in errors)
                {
                    // Display the errors that require attention
                    <li>
                        @error
                    </li>
                }
            </ul>
        </div>
    }
}
@using (Html.BeginForm())
{
     ...

【讨论】:

    猜你喜欢
    • 2013-02-28
    • 2015-08-30
    • 1970-01-01
    • 2012-03-03
    • 1970-01-01
    • 1970-01-01
    • 2015-08-02
    • 1970-01-01
    • 2011-03-22
    相关资源
    最近更新 更多