为什么文件上传没有绑定到我的视图模型? [英] Why aren't file uploads binding to my viewmodel?

查看:89
本文介绍了为什么文件上传没有绑定到我的视图模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将文件上传绑定到ViewModel(如这篇文章所示).

I'm trying to bind file uploads to a ViewModel (as demonstrated in this post).

但是我无法将文件绑定到ViewModel的Files属性.

But I can't get the files to bind to the Files property on the ViewModel.

请参见下面的代码.我在做什么错了?

Please see the code below. What am I doing wrong?

(为清楚起见,编辑-我希望上传内容绑定到VM,而不将其作为Action参数.)

ViewModel

public class PrimaryImageUploadViewModel
{
    public PrimaryImageUploadViewModel()
    {
    }

    public HttpPostedFileBase[] Files { get; set; }
    public string Title { get; set; }
}

操作

[HttpPost]
public async Task<ActionResult> Upload(PrimaryImageUploadViewModel postedModel)
{
    var requestFiles = postedModel.Files;  // THIS VALUE IS NULL - WHY?
    foreach (var f in requestFiles)
    {
        if (f.ContentLength == 0)
        {
            // do stuff
        }

    }
}

请求标头

Accept:*/*
Content-Disposition:attachment; filename="MyImage.png"
Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryEj8zSF9hwGU3ZQA9
Origin:http://localhost:52588
Referer:http://localhost:52588/example/edit/1
User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36
X-Requested-With:XMLHttpRequest

请求有效载荷

------WebKitFormBoundaryEj8zSF9hwGU3ZQA9
Content-Disposition: form-data; name="Title"

Some Test Title
------WebKitFormBoundaryEj8zSF9hwGU3ZQA9
Content-Disposition: form-data; name="files[]"; filename="MyImage.png"
Content-Type: image/png


------WebKitFormBoundaryEj8zSF9hwGU3ZQA9--

推荐答案

 public class Model1
    {
        public HttpPostedFileBase[] Files { get; set; }
        public string Title { get; set; }
    }

希望这对您有帮助...

Hope this helps...

这篇关于为什么文件上传没有绑定到我的视图模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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