0

由于某种原因,参数OriginalLocation始终null在以下代码中。我究竟做错了什么?

控制器:

 [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;
    }
4

1 回答 1

0

我认为您只需要从 Action 方法中删除 FileModel 模型参数。没有任何东西被传递给它,因此它搞砸了模型绑定(除非您从帖子中删除的视图中有更多代码)。

于 2010-08-25T20:02:41.563 回答