Reputation: 5049
My first .Net/MVC project, I generated a view that allows me to list, edit, and create items in a database, and I would like to add a file upload control to the Create page, to just upload one file.
I understand that within [HttpPost] I need "public ActionResult Index(HttpPostedFileBase file)" but my current [HttpPost] is like this: "public ActionResult Create(lm_pics lm_pics)".
Upvotes: 0
Views: 1488
Reputation: 1039588
Why do you have 2 file inputs? Why do you have 2 forms? Isn't the first form sufficient (assuming of course you add the enctype="multipart/form-data"
attribute to it as you cannot upload files without it)?:
@model PictureUploader_MVC.Models.lm_pics
@{
ViewBag.Title = "Create";
}
<h2>Upload Picture</h2>
@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.ValidationSummary(true)
<fieldset>
<legend>lmit_pics</legend>
<div class="editor-label">
@Html.LabelFor(model => model.brand)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.brand)
@Html.ValidationMessageFor(model => model.brand)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.enabled)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.enabled)
@Html.ValidationMessageFor(model => model.enabled)
</div>
<div>
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
</div>
</fieldset>
<button type="submit">Create</button>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
and your controller action that will process the form submission:
[HttpPost]
public ActionResult Create(lm_pics lm_pics, HttpPostedFileBase file)
{
...
}
Upvotes: 2