Search code examples
c#asp.netasp.net-mvcrazorrazorengine

C# Razor View Error - Braces Expected


Apologies, if this is very easy for some of you (if not everyone). I am a newbie in Razor view, so your help and support is highly appreciated.

I am receiving the following error:

} expected

in the Line 1. Here: @model ForExample.Models.DataClass

My Razor code:

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

Any idea what I might be missing? Thank you in advance!


Solution

  • You don't have to specify the switch to razor (@ character) after certain constructs

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

    After the first @if, you are in Razor markup, hence:

    1. You don't need that @, because you are still in Razor
    2. Same as point 1
    3. As you can see, you are still in Razor (otherwise, that would be invalid HTML)
    4. Here you do need @ because you moved to HTML

    So, your View fixed should look like this:

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

    You might want to check why you first use Model.Title and lines after you check Model != null.