传递HttpPostedFileBase到控制器的方法 [英] Passing a HttpPostedFileBase to a controller method

查看:1067
本文介绍了传递HttpPostedFileBase到控制器的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想创建一个表单,我可以输入姓名和上传文件。这里的视图模型:

I'm just trying to create a form where I can enter a name and upload a file. Here's the view model:

public class EmployeeViewModel
{
    [ScaffoldColumn(false)]
    public int EmployeeId { get; set; }

    public string Name { get; set; }

    public HttpPostedFileBase Resume { get; set; }
}

我的观点:

@using (Html.BeginForm("Create", "Employees", FormMethod.Post))
{   
    @Html.TextBoxFor(model => model.Name)

    @Html.TextBoxFor(model => model.Resume, new { type = "file" })

    <p>
        <input type="submit" value="Save" />
    </p>

    @Html.ValidationSummary()
}

和我控制器的方法:

[HttpPost]
public ActionResult Create(EmployeeViewModel viewModel)
{
    // code here...
}

问题是,当我张贴到控制器的方法,简历属性为null。 Name属性获取传递就好了,但不是HttpPostedFileBase。

The problem is that when I post to the controller method, the Resume property is null. The Name property gets passed just fine, but not the HttpPostedFileBase.

难道我做错了吗?

推荐答案

添加 ENCTYPE 您的形式:

@Html.BeginForm("Create", "Employees", FormMethod.Post, 
                 new{ enctype="multipart/form-data"})

这篇关于传递HttpPostedFileBase到控制器的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆