ASP.NET MVC传递模型* *一起用文件传回控制器 [英] ASP.NET MVC passing Model *together* with files back to controller

查看:135
本文介绍了ASP.NET MVC传递模型* *一起用文件传回控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我已经在这个持续了几个小时,我根本无法找到解决方案。

Ok, I've been going at this for several hours and I simply cannot find the solution.

我想从我的用户获取一些数据。因此,首先,我用一个控制器来创建一个接收模型的视图:

I want to get some data from my user. So first, I use a controller to create a view which receives a Model:

public ViewResult CreateArticle()
{
    Article newArticle = new Article();
    ImagesUploadModel dataFromUser = new ImagesUploadModel(newArticle);
    return View(dataFromUser);
}

然后,我的观点:

Then, I have the view:

<asp:Content ID="Content1" ContentPlaceHolderID="MainContentPlaceHolder" runat="server">

    <h2>AddArticle</h2>

    <% using (Html.BeginForm("CreateArticle", "Admin", FormMethod.Post, new { enctype = "multipart/form-data" })){ %>


                <%= Html.LabelFor(model => model.newArticle.Title)%>
                <%= Html.TextBoxFor(model => model.newArticle.Title)%>

                <%= Html.LabelFor(model => model.newArticle.ContentText)%>
                <%= Html.TextBoxFor(model => model.newArticle.ContentText)%>

                <%= Html.LabelFor(model => model.newArticle.CategoryID)%>
                <%= Html.TextBoxFor(model => model.newArticle.CategoryID)%>

                <p>
                    Image1: <input type="file" name="file1" id="file1" />
                </p>
                <p>
                    Image2: <input type="file" name="file2" id="file2" />
                </p>

            <div>
                <button type="submit" />Create
            </div>



    <%} %>


</asp:Content>

和最后 - 原始控制器,但这次配置为接受数据

and finally - the original controller, but this time configured to accept the data:

   [HttpPost]
    public ActionResult CreateArticle(ImagesUploadModel dataFromUser)
    {
        if (ModelState.IsValid)
        {
            HttpPostedFileBase[] imagesArr;
            imagesArr = new HttpPostedFileBase[2]; 
            int i = 0;
            foreach (string f in Request.Files)
            {
                HttpPostedFileBase file = Request.Files[f];
                if (file.ContentLength > 0)
                    imagesArr[i] = file;
            }

该控制器的其余部分,因为没有不管我做什么,在计数属性 Request.Files (或 Request.Files.Keys )保持为0。我根本无法找到一个方法来从表单传递文件(模型通过就好了)。

The rest of this controller does not matter since no matter what I do, the count attribute of Request.Files (or Request.Files.Keys) remains 0. I simply can't find a way to pass the files from the form (the Model passes just fine).

推荐答案

您可能要考虑不与形式 - 其余张贴文件有<一个href=\"http://stackoverflow.com/questions/851318/asp-net-mvc-1-to-many-saving-post-and-upload-files/851326#851326\">good原因等方式你可以达到你想要的东西。

You might want to consider not posting the files with the rest of the form- there are good reasons and other ways you can achieve what you want.

此外,检查<一href=\"http://stackoverflow.com/questions/1653469/how-can-i-upload-a-file-and-save-it-to-a-stream-for-further-$p$pview-using-c/1653508#1653508\">this问题和<一个href=\"http://www.hanselman.com/blog/ABackToBasicsCaseStudyImplementingHTTPFileUploadWithASPNETMVCIncludingTestsAndMocks.aspx\"相对=nofollow>这条建议在关于MVC文件上传

Also, check out this question and this advice regarding file uploads in MVC.

这篇关于ASP.NET MVC传递模型* *一起用文件传回控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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