尝试验证页面上的表单数据时,出现以下错误:
System.ArgumentOutOfRangeException:索引超出范围。必须是非负数且小于集合的大小。
参数名称:索引。
我这个页面的代码是:
@{
var db= Database.Open("Games");
var sqlQ = "SELECT * FROM Games";
var data = db.Query(sqlQ);
Page.Title = "Add Game";
}
@{
var fileerrorMessage = "";
var NameerrorMessage = "";
var Gamefile = "";
var GameName = "";
var fileData = Request.Files[0];
var fileName = Path.GetFileName(fileData.FileName);
var fileSavePath = Server.MapPath("~/upload/" + fileName);
GameName=Request["formName"];
Gamefile=fileName;
if (IsPost) {
var isValid = true;
if (Gamefile.IsEmpty()){
fileerrorMessage = "Please upload a file.";
isValid = false;
}
else if (GameName.IsEmpty()){
NameerrorMessage = "Please give the game a name.";
isValid = false;
}
if (isValid){
fileData.SaveAs(fileSavePath);
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" />
@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>
错误显然与第 12 行有关。这意味着应该有问题:var fileData = Request.Files[0];