Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Yes you can add AliRıza Adıyahşi.</p> <p>Here is the property to do it:</p> <pre><code>public HttpPostedFileBase File { get; set; } </code></pre> <p>Now in you form you should add <code>enctype</code> as Xiaochuan Ma said:</p> <pre><code>@using (Html.BeginForm("_AddAuthor", "Authors", FormMethod.Post, new { id = "form_author", enctype="multipart/form-data" })) { &lt;div class="editor-label"&gt; &lt;input type="file" name="file" id="file" /&gt; &lt;/div&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.Name) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.Name) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.ValidationMessageFor(model =&gt; model.Name) &lt;/div&gt; ... &lt;div class="submit-field"&gt; &lt;input type="submit" value="Ekle" class="button_gray" /&gt; &lt;/div&gt; } </code></pre> <p>On you Controller action:</p> <pre><code>[HttpPost] public ActionResult _AddAuthor(AuthorViewModel viewModel, HttpPostedFileBase file) { if(file!=null) { viewModel.File=file; //Binding your file to viewModel property } //Now you can check for model state is valid or not. if(ModelState.IsValid) { //do something } else { return View(viewModel); } } </code></pre> <p>Hope it helps. Is this what you need ?</p> <hr> <p><strong>EDIT</strong></p> <p>There is nothing additional to do. Its automatically binding to viewModel.</p> <pre><code>[HttpPost] public ActionResult _AddAuthor(AuthorViewModel viewModel) { var uploadedfile = viewModel.File;// Here you can get the uploaded file. //Now you can check for model state is valid or not. if(ModelState.IsValid) { //do something } else { return View(viewModel); } } </code></pre> <p>This worked for me !</p>
 

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