Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MVC HttpPostedFilebase始终为retuen null_C#_Razor_File Upload_Asp.net Mvc 5_Httppostedfilebase - Fatal编程技术网

C# MVC HttpPostedFilebase始终为retuen null

C# MVC HttpPostedFilebase始终为retuen null,c#,razor,file-upload,asp.net-mvc-5,httppostedfilebase,C#,Razor,File Upload,Asp.net Mvc 5,Httppostedfilebase,我试图使用以下代码在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

我试图使用以下代码在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>
Mvc行动:

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

    public ActionResult SendMessage(SendMessageModel model)
    {

    }

在方法中添加HttpPostedFileBase参数

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

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

@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>    
}  
因此,您可以获取文件名,并在Uploaded文件夹中保存您的文件。
希望它能对您有所帮助

否,它仍然返回null。编辑表单,从我的cshtml文件和ViewModel中编辑此表单,您可以看到它,我有以下@Html.TextBoxFor(m=>m.FileNew,new{type=“file”,@class=“form control”,name=“FileNew”})表单字段名为:FileNew,模型属性也为FileNewChange@Html.TextBoxFor(m=>m.FileNew,new{type=“file”,@class=“Form control”,name=“FileNew”})为否,仍然返回null。如何提交表单?将发送消息更改为可能会有所帮助。
@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();  
        }  
     }