使用模型绑定在MVC3中上载多个文件 [英] Upload multiple files in MVC3 with model binding

查看:86
本文介绍了使用模型绑定在MVC3中上载多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想对我的记录集合进行分类.我也想学习一些MVC.因此,我决定在MVC中建立一个唱片目录网站.这就是我的工作方式.

So I want to catalogue my record collection. I also want to learn some MVC. So I decided to build a record cataloguing website in MVC. That's how I work.

我只是想尽办法,但无法弄清楚如何将多个文件上传到我的SQLCE数据库.我对这里的选项很开放-将图像存储为BLOBS或仅存储为文件名,然后将图像上传到文件系统.

I'm just dipping my toes in but cannot figure out how to upload multiple files to my SQLCE database. I'm open to options here - store the images as BLOBS or simply as filenames and upload the images to the filesystem.

我的简单模型是这样:

public class Record
{
    [ScaffoldColumn(false)]
    public int RecordId { get; set; }
    [Required(ErrorMessage = "Artist is required")]
    public string Artist { get; set; }
    [Required(ErrorMessage = "Title is required")]
    public string Title { get; set; }
    [DisplayName("Release Date")]
    [DisplayFormat(DataFormatString = "{0:d}")]
    public DateTime ReleaseDate { get; set; }
    [Required(ErrorMessage = "Format is required")]
    public string Format { get; set; }
    public string Label { get; set; }
    [DisplayName("Catalogue Number")]
    public string CatalogueNumber { get; set; }
    public string Matrix { get; set; }
    public string Country { get; set; }
    public IEnumerable<HttpPostedFileBase> Images { get; set; }
    public string Notes { get; set; }
}

我的观点是:

@model Records.Models.Record
@{
    ViewBag.Title = "Create";
}
<h2>Create</h2>

@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
    <legend>Record</legend>

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

    // snipped for brevity

    <div class="editor-label">
        @Html.LabelFor(model => model.Notes)
    </div>
    <div class="editor-field">
        @Html.TextAreaFor(model => model.Notes)
        @Html.ValidationMessageFor(model => model.Notes)
    </div>
    <div class="editor-field">
       <input type="file" name="images" id="image1"/>
       <input type="file" name="images" id="image2"/>
       <input type="file" name="images" id="image3"/>
    </div>

    <p>
        <input type="submit" value="Create" />
    </p>
</fieldset>
}
<div>
    @Html.ActionLink("Back to List", "Index")
</div>

而我的Create方法是:

and my Create method is:

 [HttpPost]
    public ActionResult Create(Record record, IEnumerable<HttpPostedFileBase> images)
    {
        if (ModelState.IsValid)
        {
            foreach (HttpPostedFileBase image in images)
            {
                if (image.ContentLength > 0)
                {
                    var fileName = Path.GetFileName(image.FileName);
                    var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
                    image.SaveAs(path);
                } 
            }

            db.Records.Add(record);
            db.SaveChanges();
            return RedirectToAction("Index");  
        }

        return View(record);
    }

但是,图像(我的Create方法参数)始终为null,而Model.IsValid始终为false.

However, images (my Create method param) is always null and Model.IsValid is always false.

这是为什么?我尝试将图像上传输入命名为"image","Image","image [n]",并且images始终为0.

Why is this? I've tried naming my image upload input as 'image', 'Image', 'image[n]' and images is always 0.

我真的不想为此使用任何插件,有没有一种简单的本机MVC方式?我对助手开放!

I don't really want to use any plugin for this, is there a simple native MVC way? I'm open to helpers!

谢谢.

推荐答案

我以前有过.这可能是解决方法.

I've had this before. This might be the fix.

好像您在视图(@using (Html.BeginForm()))中缺少multipart/form-data

Looks like you are missing the multipart/form-data in your view (@using (Html.BeginForm()))

尝试将@using (Html.BeginForm())替换为 @using (Html.BeginForm("action", "controller", FormMethod.Post, new { enctype = "multipart/form-data" }))

显然要用您的操作和控制器代替.

Obviously replace action and controller with yours.

希望这会有所帮助〜会

对不起,刚刚看到了发布的日期,如果您还没有弄清楚,那么也许是这样吗?

Sorry, just seen the date posted, if you havn't figured it out then maybe this was it?

这篇关于使用模型绑定在MVC3中上载多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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