使用自定义验证的MVC文件上载为空

时间:2013-06-03 12:46:51

标签: asp.net asp.net-mvc entity-framework

我上传并根据这篇文章验证其有效性: How to validate uploaded file in ASP.NET MVC? 但是,我的示例略有不同,因为我不只是收到文件,我也收到了我的模型的一些属性。但是,我的验证器总是触发,我调试并发现我的文件始终为null,因此验证器总是会返回'false'。我不明白为什么,我在视野中的输入似乎是正确的。有什么想法吗?

namespace PhotoManagement.Models
{
public class Photo
{
    public virtual int PhotoId { get; set; }
    public virtual int ClientId { get; set; }
    public virtual string PhotoDescription { get; set; }
    [ImageValidation(ErrorMessage="Please select a PNG/JPEG image smaller than 10 MB")]
    [NotMapped]
    public HttpPostedFileBase File { get; set; }
}
}

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(Photo photo)
    {
        if (ModelState.IsValid)
        {
            db.Photos.Add(photo);
            db.SaveChanges();
            // File upload occurs now
            var FilePath = Path.Combine(Server.MapPath("~/App_Data/" + photo.ClientId), photo.PhotoId.ToString());
            photo.File.SaveAs(FilePath);
            return RedirectToAction("Create");
        }
        else return View();
    }

@using (Html.BeginForm(new { enctype = "multipart/form-data" })) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)

<fieldset>
    <legend>Photo for @Session["Name"]</legend>

    <div class="editor-field">
        @Html.Hidden("ClientId",(int)Session["UserId"])
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.PhotoDescription)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.PhotoDescription)
        @Html.ValidationMessageFor(model => model.PhotoDescription)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.File)
    </div>
    <div class="editor-field">
        <input type="file" name="File" id="File"/>
        @Html.ValidationMessageFor(model => model.File)
    </div>

3 个答案:

答案 0 :(得分:1)

您正在使用Html.BeginForm帮助程序的错误重载

正确的电话是:

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

}

你在打电话:

Html.BeginForm(object routeValues)

而不是:

Html.BeginForm(
    string actionName, 
    string controllerName, 
    FormMethod method, 
    object htmlAttributes
)

查看浏览器中生成的标记,您将看到根本区别。

答案 1 :(得分:0)

而不是

public ActionResult Create(Photo photo)

尝试

public ActionResult Create(Photo photo, HttpPostedFileBase file)

编辑:不要忘记在视图中将HTTP方法设置为POST:

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

答案 2 :(得分:-1)

Model中的文件将始终为null。为了获取文件:

[HttpPost]
        public ActionResult Create(UserViewModel model, 
FormCollection formCollection, HttpPostedFileBase file){

     /* Your code here */
    if(file==null)
    {
        ModelState.AddModelError("NoFile", "Upload File");
    }
}

此处 HttpPostedFileBase文件将为您提供上传文件的完整对象。您可以在目标文件上检查条件。不要忘记在视图中添加下面提到的验证消息。

@Html.ValidationMessage("NoFile")