【问题标题】:MVC HttpPostedFilebase is always retuen nullMVC HttpPostedFilebase 总是返回 null
【发布时间】:2019-09-17 20:50:44
【问题描述】:

我正在尝试使用以下代码在 asp.net mvc 5 应用程序中上传文件。

请查看 cshtml 和模型类并指导我缺少什么。它总是为 HttpPostedFilebase 返回 null 。

cshtml:

<form action="@Url.Action("send-message", "users", new { area = "security" })" class="form-horizontal" method="POST" data-async data-target="#send-message-modal" enctype="multipart/form-data">
    <div class="row">
        <div class="col-md-12">
            @Html.TextBoxFor(m => m.Email, new { placeholder = "Select the recipient...", @class = "form-control form-group-margin" })

            @Html.HiddenFor(m => m.RecipientId)
        </div>
    </div>

    @Html.TextBoxFor(m => m.Title, new { placeholder = "Enter a subject", @class = "form-control" })
    @Html.ValidationMessageFor(m => m.Title)


    @Html.TextAreaFor(m => m.Message, new { placeholder = "Enter your message", @class = "form-control", rows = 5 })
    @Html.ValidationMessageFor(m => m.Message)

    @Html.TextBoxFor(m => m.FileNew, new { type = "file", @class = "form-control", name= "FileNew" })
    <br/>

    @Html.ValidationMessageFor(m => m.FileNew)

    <div class="panel-footer text-right no-padding-hr no-padding-b">
        <button class="btn btn-primary">Send message</button>
    </div>
</form>

视图模型:

public class SendMessageModel
{

    [Display(Name = "Recipient")]
    [DataType(DataType.EmailAddress)]
    [StringLength(255)]
    public string Email { get; set; }

    [Required]
    [Display(Name = "Recipient")]
    public int RecipientId { get; set; }

    [DataType(DataType.Text)]
    [Required]
    [Display(Name = "Message")]
    public string Message { get; set; }

    public bool IsSent { get; set; }

    [DataType(DataType.Text)]
    [Required]
    [Display(Name = "Subject")]
    public string Title { get; set; }

    public HttpPostedFileBase FileNew { get; set; }
}

Mvc 动作:

[ActionName("send-message")]
    [HttpPost]

    public ActionResult SendMessage(SendMessageModel model)
    {

    }

【问题讨论】:

  • 如何提交表单?将 更改为 可能会有所帮助。

标签: c# razor file-upload asp.net-mvc-5 httppostedfilebase


【解决方案1】:

在查看页面,你可以添加下面的代码行

@using(Html.BeginForm("UploadFile","Upload", FormMethod.Post, new { 
enctype="multipart/form-data"}))  
{    
<div>  
    @Html.TextBox("file", "", new {  type= "file"}) <br />  

    <input type="submit" value="Upload" />  

    @ViewBag.Message  

</div>    
}  

在控制器中

public ActionResult UploadFile(HttpPostedFileBase file, SendMessageModel model)  
    {  
        try  
        {  
            if (file.ContentLength > 0)  
            {  
                string _FileName = Path.GetFileName(file.FileName);  
                string _path = Path.Combine(Server.MapPath("~/UploadedFiles"), _FileName);  
                file.SaveAs(_path);  
            }  
            ViewBag.Message = "File Uploaded Successfully!!";  
            return View();  
        }  
        catch  
        {  
            ViewBag.Message = "File upload failed!!";  
            return View();  
        }  
     }   

这样您就可以获得文件名并在 Uploaded 文件夹中保存您的文件。 希望对你有帮助

【讨论】:

    【解决方案2】:

    在方法中添加 HttpPostedFileBase 参数。

    public ActionResult SendMessage(SendMessageModel model, HttpPostedFileBase FileNew)
    {}
    

    【讨论】:

    • 不,它仍然返回null。
    • 编辑表单与此anwser stackoverflow.com/questions/8551528/…相同
    • 从我的 cshtml 文件和 ViewModel 中,你可以看到它,我有以下 @Html.TextBoxFor(m => m.FileNew, new { type = "file", @class= "form -control", name="FileNew" }) 表单字段名称为:FileNew,模型属性也为 FileNew
    • 将@Html.TextBoxFor(m => m.FileNew, new { type = "file", @class= "form-control", name= "FileNew" }) 更改为
    • 不,仍然返回 null。
    猜你喜欢
    • 2014-06-29
    • 2016-04-08
    • 2012-01-23
    • 2017-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多