Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET MVC passing Model *together* with files back to controller
    primarykey
    data
    text
    <p>Ok, I've been going at this for several hours and I simply cannot find the solution.</p> <p>I want to get some data from my user. So first, I use a controller to create a view which receives a Model:</p> <pre><code>public ViewResult CreateArticle() { Article newArticle = new Article(); ImagesUploadModel dataFromUser = new ImagesUploadModel(newArticle); return View(dataFromUser); } </code></pre> <p>Then, I have the view:</p> <pre><code>&lt;asp:Content ID="Content1" ContentPlaceHolderID="MainContentPlaceHolder" runat="server"&gt; &lt;h2&gt;AddArticle&lt;/h2&gt; &lt;% using (Html.BeginForm("CreateArticle", "Admin", FormMethod.Post, new { enctype = "multipart/form-data" })){ %&gt; &lt;%= Html.LabelFor(model =&gt; model.newArticle.Title)%&gt; &lt;%= Html.TextBoxFor(model =&gt; model.newArticle.Title)%&gt; &lt;%= Html.LabelFor(model =&gt; model.newArticle.ContentText)%&gt; &lt;%= Html.TextBoxFor(model =&gt; model.newArticle.ContentText)%&gt; &lt;%= Html.LabelFor(model =&gt; model.newArticle.CategoryID)%&gt; &lt;%= Html.TextBoxFor(model =&gt; model.newArticle.CategoryID)%&gt; &lt;p&gt; Image1: &lt;input type="file" name="file1" id="file1" /&gt; &lt;/p&gt; &lt;p&gt; Image2: &lt;input type="file" name="file2" id="file2" /&gt; &lt;/p&gt; &lt;div&gt; &lt;button type="submit" /&gt;Create &lt;/div&gt; &lt;%} %&gt; &lt;/asp:Content&gt; </code></pre> <p>and finally - the original controller, but this time configured to accept the data:</p> <pre><code> [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 &gt; 0) imagesArr[i] = file; } </code></pre> <p>The rest of this controller does not matter since no matter what I do, the <code>count</code> attribute of <code>Request.Files</code> (or <code>Request.Files.Keys</code>) remains 0. I simply can't find a way to pass the files from the form (the Model passes just fine).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload