我在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>
该错误可以指出CourseName为null,尝试将其更改为:
cmd.Parameters.AddWithValue("@CourseName", CourseName ?? "");
如果不允许null,则必须检查模型绑定器或模型验证。
关于CourseName值,如果CourseName为null,则将其更改为string.Empty!