Like you see in my diagram each Post
can have multiple PostImage
s.
How can I upload images in ASP.NET MVC? I don't know how many images can have each post, so I don't like my approach:
<label for="file1">Filename:</label>
<input type="file" name="files" id="file1" />
<label for="file2">Filename:</label>
<input type="file" name="files" id="file2" />
And after I uploaded an Image I want to have a preview of the image,
This is what I have so far.
@using (Html.BeginForm("Edit", "BlogPost", FormMethod.Post, new { nctype="multipart/form-data" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>Post</legend>
@Html.HiddenFor(model => model.ID)
<div class="editor-label">
@Html.LabelFor(model => model.Title)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Title)
@Html.ValidationMessageFor(model => model.Title)
</div>
<label for="file1">Filename:</label>
<input type="file" name="files" id="file1" />
<label for="file2">Filename:</label>
<input type="file" name="files" id="file2" />
<input type="submit" />
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
Controller:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(Post post, int[] selectedCategories, IEnumerable<HttpPostedFileBase> files)
{
if (ModelState.IsValid)
{
//Code to Edit
}
}