代码之家  ›  专栏  ›  技术社区  ›  user8719628

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

  •  0
  • user8719628  · 技术社区  · 8 年前

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

    我收到以下错误:

    }预期的

    在第1行中。此处:@模型示例。模型。数据类

    我的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>
    }
    

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

    2 回复  |  直到 8 年前
        1
  •  2
  •   Camilo Terevinto Chase R Lewis    8 年前

    您不必指定切换到razor( @ 字符)在特定构造之后

    @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. 你不需要这个 @ ,因为你还在剃刀里
    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
  •  1
  •   Shyju    8 年前

    此行

    var messageString = @ViewBag.Message;
    

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

    额外的逐字记录 @ 是您错误的原因。去掉这个,一切都会好起来的。

    您可能希望执行null,而不是检查空字符串 iewBag.Message . 使命感 Contains 关于值的方法 ViewBag.Message 当它是 NULL 将引发异常( 无法对空引用执行运行时绑定 )

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

    我还注意到,您正在创建输入元素,并根据模型的属性设置值属性值。您可以考虑 Html.TextBoxFor helper方法,它将为您生成相同的

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