我在ASP.NET MVC中上传视频时遇到问题。我正在使用HttpPostedfile
基类来上传视频,并且我有一个用于视频的模型来添加额外的字段。我收到此错误,不知道如何解决。
我得到的错误是;
System.Data.SqlClient.SqlException:过程或函数“ sAddNewVideoFile”需要未提供的参数“ @CourseName”。
这是我的代码
型号
namespace eLearning.Models
{
public class FileModel
{
public int ID { get; set; }
public string Name { get; set; }
public Nullable<int> FileSize { get; set; }
public string FilePath { get; set; }
public int CourseID { get; set; }
public string CourseName { get; set; }
}
}
控制器
public ActionResult UploadVideos()
{
return View();
}
[HttpPost]
public ActionResult UploadVideos(HttpPostedFileBase fileupload)
{
if (fileupload != null)
{
var file = new FileModel();
string fileName = Path.GetFileName(fileupload.FileName);
int fileSize = fileupload.ContentLength;
int Size = fileSize / 1000;
fileupload.SaveAs(Server.MapPath("~/VideoFileUpload/" + fileName));
int CourseId = file.CourseID;
string CourseName = file.CourseName;
string CS = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
SqlCommand cmd = new SqlCommand("sAddNewVideoFile", con);
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
cmd.Parameters.AddWithValue("@Name", fileName);
cmd.Parameters.AddWithValue("@FileSize", Size);
cmd.Parameters.AddWithValue("@FilePath", "~/VideoFileUpload/" + fileName);
cmd.Parameters.AddWithValue("@CourseID", CourseId);
cmd.Parameters.AddWithValue("@CourseName", CourseName);
cmd.ExecuteNonQuery();
}
}
return View();
}
存储过程:
CREATE procedure [dbo].[sAddNewVideoFile]
(@Name NVARCHAR(50),
@FileSize INT,
@FilePath NVARCHAR(100),
@CourseID INT,
@CourseName NVARCHAR(50)
)
AS
BEGIN
INSERT INTO Files (Name, FileSize, FilePath, CourseID, CourseName)
VALUES (@Name, @FileSize, @FilePath, @CourseID, @CourseName)
END
查看
@model eLearning.Models.FileModel
@{
ViewBag.Title = "UploadVideos";
}
<h2>UploadVideo</h2>
<!DOCTYPE html>
<html>
<body>
<div class="container py-4">
<div class="card">
<div class="card-header bg-danger text-white">
<h6 class="text-uppercase">video List</h6>
</div>
<div class="card-body">
<div class="row">
<button style="margin-left: 27px; margin-bottom:10px;" type="button" class="btn btn-danger rounded-0" data-toggle="modal" data-target="#UploadVideo">
<i class="fa fa-plus-circle"></i> Add New
</button>
<div class="modal fade" id="UploadVideo">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Upload New video File</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<div class="modal-body">
@using (Html.BeginForm("UploadVideos", "File", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div class="form-group">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.CourseID, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CourseID, new { htmlAttributes = new { @class = "form-control" } })
</div>
@Html.LabelFor(model => model.CourseName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CourseName, new { htmlAttributes = new { @class = "form-control" } })
</div>
</div>>
<label>Choose File:</label>
<div class="input-group">
<div class="custom-file">
<input type="file" id="fileupload" name="fileupload" class="custom-file-input" />
<label class="custom-file-label"></label>
</div>
<div class="input-group-append">
<input type="submit" id="btnUpload" class="btn btn-secondary" value="Upload" />
</div>
</div>
</div>
}
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
答案 0 :(得分:2)
该错误可能指出CourseName为空,请尝试将其更改为:
INT, VARCHAR
如果不允许使用null,则必须检查模型绑定程序或模型验证。
答案 1 :(得分:0)
如果CourseName为null,则关于CourseName值的问题将其更改为string.Empty!