简体   繁体   English

MVC HttpPostedFilebase始终为null

[英]MVC HttpPostedFilebase is always retuen null

I am trying to upload a file in asp.net mvc 5 application by using the following code. 我正在尝试使用以下代码在asp.net mvc 5应用程序中上传文件。

Please see the cshtml and model class and guide me what I am missing. 请参阅cshtml和模型类,并指导我所缺少的内容。 It always return null for HttpPostedFilebase . 对于HttpPostedFilebase,它总是返回null。

cshtml : 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>

ViewModel: 视图模型:

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 Action : Mvc动作:

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

    public ActionResult SendMessage(SendMessageModel model)
    {

    }

In View page, you can add the below line of code 在“查看”页面中,您可以添加以下代码行

@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>    
}  

and in controller 并在控制器中

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();  
        }  
     }   

so you can get the file name and in the Uploadedfolder your file will be saved. 这样您就可以获取文件名,并在Uploadedfolder中保存您的文件。 Hope it can be helpful to you 希望对您有帮助

Add the HttpPostedFileBase parameter in method. 在方法中添加HttpPostedFileBase参数。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM