Home C# Razor View Error - Braces Expected
Reply: 2

C# Razor View Error - Braces Expected

Camilo Terevinto
1#
Camilo Terevinto Published in 2018-01-12 20:54:29Z

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!

Camilo Terevinto
2#
Camilo Terevinto Reply to 2018-01-12 21:15:50Z

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.

Shyju
3#
Shyju Reply to 2018-01-12 21:33:59Z

This line

var messageString = @ViewBag.Message;

is already inside a code block (opened by the if condition statement). So you do not need the extra @.

The extra verbatim @ is the cause for your error. Remove that and everything should work fine.

You probably want do a null instead of checking against empty string for iewBag.Message. Calling Contains method on the value of ViewBag.Message when it is NULL will throw you an exception (Cannot perform runtime binding on a null reference)

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

I also noticed, you are creating input elements and setting the value attribute value from the properties of the model. You may consider the Html.TextBoxFor helper method, which will generate the same for you

@Html.TextBoxFor(a=>a.Title,new { @class="form-control"})
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.301132 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO