I am trying to save an image to SQL server database using MVC application. With current setting the user can select the image file using imageUpload control and it will be shown in image1 control. For experimental purpose source to image2 control is hardcoded by giving the path to the image file. Then when the user clicks the save button the text data will be saved in the database. However, the serialization of the three controls: imageUpload, image1, and image2 controls returns null to the controller. Due to this I am not able to save the image data. Would you please provide me your suggestion to fix this problem?
Here is the code in the view file
@using (Html.BeginForm("", "EditProfile", FormMethod.Post, new { name= "EditingForm", id = "EditingForm", enctype = "multipart/form-data" }))
{
<label name="msg" id="msg" style="color:blue"></label>
@Html.TextBox("Name")
<input id="imageUpload" name="imageUpload" type="file" onchange="DisplayImage(this)" />
<img id="image1" name="image1" runat="server" height="190" width="172" />
<img id="image2" name="image2" runat="server" src="~/images/laila.png" height="190" width="172" />
<input type="button" class="btn btn-colored" value="Save Profile" onclick="SaveProfileData('e'); " />
}
Here is the code in the Javascript
function SaveProfileData(e) {
var data = $("#EditingForm").serialize();
$.post("/Api/Member/SaveMembersData", data, function (result) {
msg.innerText = result;
$("#loaderDiv").hide();
});
return false;
};
And here is the code in the cotroller
[HttpPost]
[Route("Api/Member/SaveMembersData")]
public string SaveMembersData(EditMemberRequestModel m)
{
if (m.Name == null)
{
return "Name can not be empty";
}
else
{
}
if(m.imageUpload != null)
{
}
if(m.image1 != null)
{
}
if (m.image2 != null)
{
}
}
What I have tried:
I have tried to google some related posts but that didn't help me.