0

在尝试验证我的表单时,我收到以下错误:期望一个“{”但发现一个“(”。块语句必须包含在“{”和“}”中。您不能在 CSHTML 中使用单语句控制流语句页面。我在这个页面的代码行是:

@{
var db= Database.Open("Games");
var sqlQ = "SELECT * FROM Games";
var data = db.Query(sqlQ);   
}
@{
    var fileerrorMessage = "";
    var NameerrorMessage = "";
    var Gamefile = "";
    var GameName = "";
    var isValid = true;
    if (IsPost) {
        if (file.IsEmpty()){
        fileerrorMessage = "Please upload a file.";
        isValid = false;
        }

    else (formName.IsEmpty()){
        NameerrorMessage = "Please give the game a name.";
        isValid = false;
    }

    if (isValid){
    var fileData = Request.Files[0];
    var fileName = Path.GetFileName(fileData.FileName);
    var fileSavePath = Server.MapPath("~/upload/" + fileName);
    fileData.SaveAs(fileSavePath);
    GameName=Request["formName"];
    Gamefile=fileName;
    var SQLINSERT = "INSERT INTO Games (Name, file_path) " + "VALUES (@0, @1)";
    db.Execute(SQLINSERT, GameName, Gamefile);
    Response.Redirect("default.cshtml");
    }

    else
    {
        <p class="message error">Please correct the errors and resubmit the form.</P> 
    }

    }
}
<form action="" method="post" enctype="multipart/form-data">

  <input type="file" name="file" id="file" /><p>
    @if(!fileerrorMessage.IsEmpty()) {
        <label for="file" class="validation-error">
            @fileerrorMessage
        </label>
    }
  <p><input type="text" name="formName" value="@GameName" />
     @if(!NameerrorMessage.IsEmpty()) {
        <label for="file" class="validation-error">
            @NameerrorMessage
        </label>
     }

  <input type="submit" value="Add Game" />
</form>

错误页面显示错误出现在第 18 行,这表明存在问题:if (file.IsEmpty()){

4

2 回答 2

1

我来到这里遇到了类似的问题 - 对我来说问题是:

@{
    var stuff = getSomeStuff();

    if (stuff.Whatever)
        doThing();
}
@if (stuff.Condition) {
<button/>
} else {
<a href=""></a>
}

问题是没有 if 语句,即使是上面明显代码块中的第一个语句,也不能在没有花括号的情况下使用。所以上面变成了:

@{
    var stuff = getSomeStuff();

    if (stuff.Whatever)
    {
        doThing();
    }
}
@if (stuff.Condition) {
<button/>
} else {
<a href=""></a>
}
于 2013-04-29T07:28:04.167 回答
1
else (formName.IsEmpty()){
    NameerrorMessage = "Please give the game a name.";
    isValid = false;

应该:

else if (formName.IsEmpty()){
    NameerrorMessage = "Please give the game a name.";
    isValid = false;

不见了if

于 2012-04-07T09:14:59.527 回答