How To Upload Image To Sql With Mvc3 Within One Form Using Ado.net?
I am having trouble putting image upload field within the rest of the fieldset, mainly becaouse i need two Actions for one Form, correct me if I am wrong! Aloso when using Model to
Solution 1:
In your viewmodel, declare the image field as an HttpPostedFileBase like so:
public HttpPostedFileBase ImageData { get; set; }
In the view, make sure you have both the ID and NAME attributes of the file input correspond to the name of the property on your viewmodel:
<inputtype="file" name="ImageData"id="ImageData" />
When you submit the form, the uploaded file will be populated into the HttpPostedFileBase property by the default model binder.
Also, your Html.Form needs to wrap all of the input fields, not just the file upload. Having nested forms is invalid in HTML.
@using (Html.BeginForm("AddForm", "Admin", FormMethod.Post,
new { enctype = "multipart/form-data" }))
{
<div><fieldset><legend>New Article</legend><divclass="editor-label">
@Html.LabelFor(m => m.Author)
</div><divclass="editor-field">
@Html.TextBoxFor(m => m.Author)
@Html.ValidationMessageFor(m => m.Author)
</div><divclass="editor-label">
@Html.LabelFor(m => m.Title)
</div><divclass="editor-field">
@Html.TextBoxFor(m => m.Title)
</div><divclass="editor-label">Image</div><divclass="editor-field"><div>Upload image:
<inputtype="file"name="ImageData"id="ImageData" /></div></div><p><inputtype="submit"value="Save" /></p></fieldset></div>
}
Update
I cannot answer the ADO.NET part of the question. We use EF 4.1 with the above approach, saving to a database table through an entity property. EF takes care of the ADO.NET part for us, so I'm not sure what the code would be using bare ADO.NET.
Post a Comment for "How To Upload Image To Sql With Mvc3 Within One Form Using Ado.net?"