如何在Razor视图中使用字符串列表?

时间:2011-11-18 14:27:30

标签: asp.net-mvc-3 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>

2 个答案:

答案 0 :(得分:3)

服务器端代码周围不需要@{},将ViewData值转换为List<string>。作为旁注,如果您使用了视图模型而不是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>

答案 1 :(得分:0)

问题与代码块的位置有关。将代码块移到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())
{
     ...