C# Razor 视图错误 - 需要大括号

C# Razor View Error - Braces Expected

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

我收到以下错误:

} expected

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

我的 Razor 代码:

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

知道我可能遗漏了什么吗?提前致谢!

这一行

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"})

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

@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 后使用行。