【问题标题】:C# Razor View Error - Braces ExpectedC# Razor 视图错误 - 需要大括号
【发布时间】:2018-01-12 20:54:29
【问题描述】:

抱歉,如果这对你们中的一些人(如果不是所有人)来说很容易的话。我是 Razor 视图的新手,非常感谢您的帮助和支持。

我收到以下错误:

} 预期

在第 1 行。这里:@model ForExample.Models.DataClass

我的剃刀代码:

@model ForExample.Models.DataClass

@{
    ViewBag.Title = "Edit: " + @Model.Title;
}

@if(Model != null)
{

     if (@ViewBag.Message != "")
     {      
                var messageString = @ViewBag.Message;

                if (messageString.Contains("Successfully"))
                {
                    <h3 style="color:green;">@ViewBag.Message</h3>
                }
                if(!messageString.Contains("Successfully"))
                {
                    <h3 style="color:red;">@ViewBag.Message</h3>
                }
     }

    <div>
        <form method="post">
            @Html.HiddenFor(model => model.Id)
            <h4>@Html.LabelFor(ex => ex.Title)</h4>
            <input type="text" id="Title" name="Title" class="form-control" value="@Model.Title" />
            <h4>@Html.LabelFor(ex => ex.Tags)</h4>
            <input type="text" id="Tags" name="Tags" class="form-control" value="@Model.Tags" />
            <h4>@Html.LabelFor(ex => ex.Description)</h4>
            <textarea id="Description" name="Description" class="form-control" rows="10" cols="100">@Model.Description</textarea><br />
            <button value="Example" id="Edit" class="btn btn-success btn-lg"> Save Changes </button>
        </form>
    </div>
}

知道我可能缺少什么吗?提前谢谢!

【问题讨论】:

  • if (@ViewBag.Message != "") 应该是 if (ViewBag.Message != "")

标签: c# asp.net asp.net-mvc razor razorengine


【解决方案1】:

您不必在某些构造之后指定切换到剃刀(@ 字符)

@if(Model != null)
{
    if (@ViewBag.Message != "") -> (1)
    {      
        var messageString = @ViewBag.Message;  -> (2)

        if (messageString.Contains("Successfully")) -> (3)
                <h3 style="color:green;">@ViewBag.Message</h3> -> (4)

在第一个 @if 之后,您处于 Razor 标记中,因此:

  1. 你不需要那个@,因为你还在 Razor 中
  2. 同第1点
  3. 如您所见,您仍在 Razor 中(否则,这将是无效的 HTML
  4. 这里你确实需要@,因为你搬到了HTML

因此,您的固定视图应该如下所示:

@model ForExample.Models.DataClass

@{
    ViewBag.Title = "Edit: " + Model.Title;
}

@if(Model != null)
{
    string message = ViewBag.Message as string;
    if (!string.IsNullOrEmpty(message))
    {      
        if (message.Contains("Successfully"))
        {
            <h3 style="color:green;">@message</h3>
        }
        else
        {
            <h3 style="color:red;">@message</h3>
        }
    }
}

您可能想检查为什么首先使用Model.Title 以及检查Model != null 之后的行。

【讨论】:

    【解决方案2】:

    这一行

    var messageString = @ViewBag.Message;
    

    已经在代码块中(由if 条件语句打开)。所以你不需要额外的@

    额外的逐字记录@ 是您错误的原因。删除它,一切都会正常工作。

    您可能想要执行 null 而不是检查空字符串 iewBag.Message。在ViewBag.Message 的值为NULL 时调用Contains 方法会抛出异常(无法对空引用执行运行时绑定

    @if(Model != null)
    {
        if (ViewBag.Message != null)
        {      
            var messageString = ViewBag.Message;
    
            if (messageString.Contains("Successfully"))
            {
                <h3 style="color:green;">@messageString</h3>
            }
            if(!messageString.Contains("Successfully"))
            {
                <h3 style="color:red;">@messageString</h3>
            }
        }
        <div>
            <form method="post">
                @Html.HiddenFor(model => model.Id)
                <h4>@Html.LabelFor(ex => ex.Title)</h4>
                <input type="text" id="Title" name="Title" class="form-control" value="@Model.Title" />
                <h4>@Html.LabelFor(ex => ex.Tags)</h4>
                <input type="text" id="Tags" name="Tags" class="form-control" value="@Model.Tags" />
                <h4>@Html.LabelFor(ex => ex.Description)</h4>
                <textarea id="Description" name="Description" class="form-control" rows="10" cols="100">@Model.Description</textarea><br />
                <button value="Example" id="Edit" class="btn btn-success btn-lg"> Save Changes </button>
            </form>
        </div>
    }
    

    我还注意到,您正在创建输入元素并从模型的属性中设置 value 属性值。您可以考虑使用Html.TextBoxFor 辅助方法,它会为您生成相同的方法

    @Html.TextBoxFor(a=>a.Title,new { @class="form-control"})
    

    【讨论】:

    • 你的答案几乎是正确的,顶部有一个@Model.Title; @ 无效
    • 我 99% 确信,该代码仍然可以正常工作,因为它位于单独的代码块中。也不需要空值检查,因为字符串连接会解决这个问题,因为我们在该表达式中至少有一个有效字符串 (Edit:)
    • 现在我 100% 确定它有效。我只是在本地验证了它:)
    • 这就是我要做的。我很确定 VS 对方抱怨我在 @{ } 中放了一个 @
    • 是的,VS应该会给出警告。但是代码不会崩溃
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-24
    • 2016-02-04
    相关资源
    最近更新 更多