如何在我的 MVC3 模型绑定中包含 Telerik 文件上传?

Pro*_*ofK 4 asp.net asp.net-mvc asp.net-mvc-3

除了视图模型参数之外,我不喜欢在 POST 操作方法中使用参数。但是,对于使用 Telerik Upload 帮助程序上传文件,我似乎被迫这样做。发布的值为IEnumerable<HttpPostedFileBase>。有什么方法可以将其绑定到模型,而无需进行自定义模型绑定。

Dar*_*rov 5

除了视图模型参数之外,我不喜欢在 POST 操作方法中使用参数。

我也不。这就是我使用视图模型的原因:

public class MyViewModel
{
    public IEnumerable<HttpPostedFileBase> Files { get; set; }
    public string Foo { get; set; }
    public string Bar { get; set; }
    ...
}
Run Code Online (Sandbox Code Playgroud)

进而:

[HttpPost]
public ActionResult Upload(MyViewModel model)
{
    if (!ModelState.IsValid)
    {
        return View(model);
    }

    if (model.Files != null)
    {
        foreach (var file in model.Files)
        {
            if (file != null && file.ContentLength > 0)
            {
                // process the uploaded file
            }
        }
    }
    ...
}
Run Code Online (Sandbox Code Playgroud)