Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#Razor视图错误-需要大括号_C#_Asp.net_Asp.net Mvc_Razor_Razorengine - Fatal编程技术网

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

C#Razor视图错误-需要大括号,c#,asp.net,asp.net-mvc,razor,razorengine,C#,Asp.net,Asp.net Mvc,Razor,Razorengine,道歉,如果这对你们中的一些人(如果不是每个人)来说都很容易。我是Razor视野中的新手,非常感谢您的帮助和支持 我收到以下错误: }期望 在第1行。此处:@model example.Models.DataClass 我的剃须刀代码: @model ForExample.Models.DataClass @{ ViewBag.Title = "Edit: " + @Model.Title; } @if(Model != null) { if (@ViewBag.Messa

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

我收到以下错误:

}期望

在第1行。此处:@model example.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>
}
@model-example.Models.DataClass
@{
ViewBag.Title=“编辑:”+@Model.Title;
}
@如果(型号!=null)
{
如果(@ViewBag.Message!=“”)
{      
var messageString=@ViewBag.Message;
if(messageString.Contains(“成功”))
{
@查看包。留言
}
如果(!messageString.Contains(“成功”))
{
@查看包。留言
}
}
@Html.HiddenFor(model=>model.Id)
@LabelFor(ex=>ex.Title)
@LabelFor(ex=>ex.Tags)
@LabelFor(ex=>ex.Description)
@型号说明
保存更改 }
知道我遗漏了什么吗?提前谢谢你

这条线

var messageString = @ViewBag.Message;
已在代码块内(由
if
条件语句打开)。因此,您不需要额外的
@

额外的逐字
@
是导致错误的原因。去掉那个,一切都会好起来的

您可能希望对
iewBag.Message
执行null而不是检查空字符串。调用
包含对
ViewBag值的
方法。消息
NULL
时将引发异常(无法对NULL引用执行运行时绑定

这条线

var messageString = @ViewBag.Message;
已在代码块内(由
if
条件语句打开)。因此,您不需要额外的
@

额外的逐字
@
是导致错误的原因。去掉那个,一切都会好起来的

您可能希望对
iewBag.Message
执行null而不是检查空字符串。调用
包含对
ViewBag值的
方法。消息
NULL
时将引发异常(无法对NULL引用执行运行时绑定


在某些构造之后,不必指定切换到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)

您可能需要检查为什么首先使用
Model.Title
以及检查
Model!=null

在某些构造之后,不必指定切换到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)

您可能需要检查为什么首先使用
Model.Title
以及检查
Model!=null

如果(@ViewBag.Message!=“”)应该是if(ViewBag.Message!=“”)如果(@ViewBag.Message!=“”)应该是if(ViewBag.Message!=“”)您的答案几乎正确,则有一个
@Model.Title在顶部,
@
无效。99%确定该代码仍能正常工作,因为它位于单独的代码块中。另外,不需要空检查,因为字符串连接将解决这一问题,因为在该表达式中至少有一个有效字符串(
编辑:
),现在我100%确定它可以工作。我只是在当地核实了一下:)这就是我要做的。我很确定VS另一个抱怨说我把一个
@
放在
{}
里面是的,VS应该发出警告。但是代码不会太粗。你的答案几乎是正确的,有一个
@Model.Title在顶部,
@
无效。99%确定该代码仍能正常工作,因为它位于单独的代码块中。另外,不需要空检查,因为字符串连接将解决这一问题,因为在该表达式中至少有一个有效字符串(
编辑:
),现在我100%确定它可以工作。我只是在当地核实了一下:)这就是我要做的。我很确定VS另一个抱怨说我把一个
@
放在
{}
里面是的,VS应该发出警告,但代码不会崩溃
@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>
        }
    }
}