Upload Selected File With Form Data In ASP.Net MVC - ASPsnippets

hello,

if i remove file upload control the insert works but with file upload control insert is not working

what i want to achieve is user can type all the values in text box select image thru file upload and image save in UPLOAD folder and path of file add in data base myfile column

please advice

strongly type model create form

@model BussinessObject.UserBO @{ ViewBag.Title = "Create"; } <h2>Create</h2> @using (Html.BeginForm()) { @Html.AntiForgeryToken() <div class="form-horizontal"> <h4>UserBO</h4> <hr /> @Html.ValidationSummary(true, "", new { @class = "text-danger" }) <div class="form-group"> @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Address, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Address, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Address, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.EmailID, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.EmailID, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.EmailID, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Mobilenumber, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Mobilenumber, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Mobilenumber, "", new { @class = "text-danger" }) </div> </div> <span>Select File:</span> @Html.TextBoxFor(m => m.MyFile, new { type = "file" }) <br /> @Html.ValidationMessageFor(m => m.MyFile, "", new { @class = "error" }) <hr /> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="Create" class="btn btn-default" /> </div> </div> </div> } <div> @Html.ActionLink("Back to List", "Index") </div> public ActionResult Create([Bind(Include = "userid,Name,Address,EmailID,Mobilenumber, MyFile")] UserBO userBO, HttpPostedFileBase postedFile) { if (ModelState.IsValid) { //Extract Image File Name. string fileName = System.IO.Path.GetFileName(postedFile.FileName); //Set the Image File Path. string filePath = "~/Uploads/" + fileName; //Save the Image File in Folder. postedFile.SaveAs(Server.MapPath(filePath)); userbl.SaveUserregisrationBL(userBO); //db.UserBOes.Add(userBO); //db.SaveChanges(); return RedirectToAction("Index"); } return View(userBO); }

Từ khóa » Html.editorfor Image Upload