I am a student developer in .Net MVC 5 and I want to insert image from new user to my database. I found a lot of method for byte[] data type but according to my researchs , there is no source for string data type.(Example; Entity Framework 5 Code first adding an image) . What can i do for string column of ImageUrl in my database? Could you help me at this issue ?
My Model :
public class usersModel{
public string userImageUrl{get;set;}
}
My Controller :
public class UsersController : Controller
{
AgmEntities db = new AgmEntities();
public ActionResult Create()
{
return View("Create", new usersModel());
}
[HttpPost]
public ActionResult Create(usersModel uModel,HttpPostedFileBase file)
{
if (!ModelState.IsValid)
{
return View("Create");
}
var user = new Users();
if(file !=null && file.ContentLength > 0)
{
string fileName = Path.GetFileName(file.FileName);
string imgPath = Path.Combine(Server.MapPath("~/User_Images/"), fileName);
file.SaveAs(imgPath);
}
user.userImageUrl = "~/User_Images/" + file.FileName;/*Error line*/
db.Users.Add(user);
db.SaveChanges();
return RedirectToAction("Index", "Home");
}
}
My cshtml :
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-group">
@Html.LabelFor(model => model.userImageUrl, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<input type="file" name="ImageFile" />
@Html.ValidationMessageFor(model => model.userImageUrl, "", new { @class = "text-danger" })
</div>
</div>
}