In mvc3 application, Product has properties as ImagePath1, ImagePath2, ImagePath3, ImagePath4 and ImagePath5 in string type.
I can save 1 image in this rule.
For 1 image, in the controller:
public ActionResult Create( Product product, HttpPostedFileBase file )
{
var filename = Path.GetFileName( file.FileName );
var path = Path.Combine( Server.MapPath( "~/UploadFolder/Products" ), filename );
file.SaveAs( path );
product.ImagePath1 = filename;
db.Products.AddObject( product );
db.SaveChanges();
}
I have file upload sample as:
<script type="text/javascript">
function HandleFileButtonClick() {
document.frmUpload.myFile.click();
document.frmUpload.txtFakeText.value = document.frmUpload.myFile.value;
}
</script>
<form name="frmUpload">
<input type="file" name="myFile" style="display: none;">
<input type="text" name="txtFakeText" readonly="true">
<input type="button" onclick="HandleFileButtonClick();" value="Select" class="UploadButton">
</form>
but I can not apply thit in Product’s create view.
@using ( Html.BeginForm( "Create", "Product", FormMethod.Post, new { enctype = "multipart/form-data" } ) )
{
@Html.TextBoxFor( m => m.Name, new { @class = "createInputStyle" } )
...
...// Images here...
...
<input type="submit" value="Create" class="" />
}
File upload sample works outside, but when I place upload form into Create form, have problem.
How to save more images? Sorry for bad English.
Stick the following inside your Html.BeginForm() {}
Then, on your controller, check
Request.Files, which should hold these images (providing images were uploaded)