Asp.net mvc 3 MVC3::在操作中传递对象和HttpPostedFile

Asp.net mvc 3 MVC3::在操作中传递对象和HttpPostedFile,asp.net-mvc-3,file-upload,Asp.net Mvc 3,File Upload,我在获取上传的文件(HTTPPostedFile)和发布到操作的对象时遇到问题。我有一个叫做widget的类: public class Widget { public string FirstName { get; set; } public string LastName { get; set; } public string FilePath { get; set; } } 在Widget控制器中,我有一个“Add”方法 public ActionResult A

我在获取上传的文件(HTTPPostedFile)和发布到操作的对象时遇到问题。我有一个叫做widget的类:

public class Widget
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string FilePath { get; set; }
}
在Widget控制器中,我有一个“Add”方法

public ActionResult Add()
{
    return View();
}
以及一个重载方法来接受用户发回的内容

[HttpPost]
public ActionResult Add(Widget widget, HttpPostedFile file)
{
    // Save posted file using a unique
    // Store the path/unique name in Widget.FilePath
    // Save new Widget object
    return View();
}
我认为:

@model Project.Models.Widget
@{
    using(Html.BeginForm())
    {
        Html.LabelFor(model => model.FirstName)<br />
        Html.TextBoxFor(model => model.FirstName)<br />
        Html.LabelFor(model => model.LastName)<br />
        Html.TextBoxFor(model => model.LastName)<br />
        <input type="file" id="file" /><br />
        <input type="submit" value="Save" />
    }
}
@model Project.Models.Widget
@{
使用(Html.BeginForm())
{
Html.LabelFor(model=>model.FirstName)
Html.TextBoxFor(model=>model.FirstName)
Html.LabelFor(model=>model.LastName)
Html.TextBoxFor(model=>model.LastName)

} }
我想做的是让用户填写表单并选择要上载的文件。上传文件后,我想使用唯一的名称保存文件,然后将文件路径存储为widget.FilePath

每次尝试时,都会填充widget对象,但uploadedFile为null


非常感谢您的帮助。

您的代码有几个问题

  • 确保已将适当的
    enctype=“multipart/form data”
    设置到表单中,否则将无法上载任何文件
  • 确保文件输入具有
    名称
    属性,并且该属性的值与操作参数的名称匹配。分配
    id
    对服务器端绑定没有影响
例如:

@model Project.Models.Widget
@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.LabelFor(model => model.FirstName)<br />
    @Html.TextBoxFor(model => model.FirstName)<br />
    @Html.LabelFor(model => model.LastName)<br />
    @Html.TextBoxFor(model => model.LastName)<br />
    <input type="file" id="file" name="file" /><br />
    <input type="submit" value="Save" />
}
还可以将两个参数合并到一个视图模型中:

public class Widget
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string FilePath { get; set; }
    public HttpPostedFileBase File { get; set; }
}
然后:

[HttpPost]
public ActionResult Add(Widget widget)
{
    // Save posted file using a unique
    // Store the path/unique name in Widget.FilePath
    // Save new Widget object
    return View();
}

最后阅读以下博文:

您的代码有几个问题

  • 确保已将适当的
    enctype=“multipart/form data”
    设置到表单中,否则将无法上载任何文件
  • 确保文件输入具有
    名称
    属性,并且该属性的值与操作参数的名称匹配。分配
    id
    对服务器端绑定没有影响
例如:

@model Project.Models.Widget
@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.LabelFor(model => model.FirstName)<br />
    @Html.TextBoxFor(model => model.FirstName)<br />
    @Html.LabelFor(model => model.LastName)<br />
    @Html.TextBoxFor(model => model.LastName)<br />
    <input type="file" id="file" name="file" /><br />
    <input type="submit" value="Save" />
}
还可以将两个参数合并到一个视图模型中:

public class Widget
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string FilePath { get; set; }
    public HttpPostedFileBase File { get; set; }
}
然后:

[HttpPost]
public ActionResult Add(Widget widget)
{
    // Save posted file using a unique
    // Store the path/unique name in Widget.FilePath
    // Save new Widget object
    return View();
}

最后阅读以下博文:

HttpPostedFileBase——这是我的问题HttpPostedFileBase——这是我的问题