在尝试验证我的表单时,我收到以下错误:期望一个“{”但发现一个“(”。块语句必须包含在“{”和“}”中。您不能在 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()){