C# 当我尝试在ASP.NET MVC中上载文件时,它显示为null
由于某些原因,以下代码中的参数C# 当我尝试在ASP.NET MVC中上载文件时,它显示为null,c#,.net,asp.net-mvc,httppostedfilebase,C#,.net,Asp.net Mvc,Httppostedfilebase,由于某些原因,以下代码中的参数OriginalLocation始终为null。我做错了什么 控制器: 视图: 我认为您只需要从Action方法中删除FileModel参数。没有任何东西传递给它,因此它破坏了模型绑定(除非视图中有更多的代码,您已经从帖子中删除了这些代码) [HttpPost] public ActionResult File(HttpPostedFileBase OriginalLocation, FileModel model) { byte[] binaryD
OriginalLocation
始终为null
。我做错了什么
控制器:
视图:
我认为您只需要从Action方法中删除FileModel参数。没有任何东西传递给它,因此它破坏了模型绑定(除非视图中有更多的代码,您已经从帖子中删除了这些代码)
[HttpPost]
public ActionResult File(HttpPostedFileBase OriginalLocation, FileModel model)
{
byte[] binaryData = null;
if (OriginalLocation != null && OriginalLocation.ContentLength > 0)
{
binaryData = new byte[OriginalLocation.ContentLength];
OriginalLocation.InputStream.Read(binaryData, 0,
OriginalLocation.ContentLength);
if (model.UploadFile(OriginalLocation))
{
return RedirectToAction("Profile", "Account");
}
}
return View();
}
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/MasterPage.Master" Inherits="System.Web.Mvc.ViewPage<NISE.Web.TestForum.Models.File.FileModel>" %>
<asp:Content ID="Content2" ContentPlaceHolderID="head" runat="server">
Find upload
</asp:Content>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContentPlaceHolder" runat="server">
<% using (Html.BeginForm("File", "File", FormMethod.Post, new { enctype = "multipart/form-data" }))
{ %>
<%--<form enctype="multipart/form-data" method="post" action="/File/File">--%>
<input type="file" name="OriginalLocation" id="OriginalLocation" />
<input type="submit" value="Upload" />
<%--</form>--%>
<%} %>
</asp:Content>
public bool UploadFile(HttpPostedFileBase OriginalLocation)
{
if (OriginalLocation != null)
{
var filename = Path.GetFileName(OriginalLocation.FileName);
OriginalLocation.SaveAs(@"C:\" + filename);
return true;
}
return false;
}