sonal malhotra

sonal malhotra

  • NA
  • 410
  • 51.7k

EDit Image code is not working using ef in mvc ?

Mar 8 2019 5:31 AM
hi  have shown code  edit image in controller.
 
public partial class Itemtable
{
public int ItemId { get; set; }
public int CategoryId { get; set; }
public int ProducerId { get; set; }
public string Title { get; set; }
public decimal Price { get; set; }
public string ItemUrl { get; set; }
public string CategoryName { get; set; }
public string Productname { get; set; }
public HttpPostedFileBase Imagefile { get; set; }
public HttpPostedFileBase Imagefile1 { get; set; }
 
public ActionResult Edit([Bind(Include = "ItemId,CategoryId,ProducerId,Title,Price,ItemUrl")] Itemtable itemtable)
{
List<SelectListItem> allcategory = new List<SelectListItem>();
foreach (var role in db.categoryTables)
{
allcategory.Add(new SelectListItem { Text = role.CategoryName, Value = role.CategoryId.ToString() });
}
ViewBag.category = allcategory;
List<SelectListItem> allproduct = new List<SelectListItem>();
foreach (var role in db.prodtbls)
{
allproduct.Add(new SelectListItem { Text = role.Productname, Value = role.ProducerId.ToString() });
}
ViewBag.product = allproduct;
string filename1 = Path.GetFileNameWithoutExtension(itemtable.Imagefile1.FileName);
string extension1 = Path.GetExtension(itemtable.Imagefile1.FileName);
filename1 = filename1 + extension1;
itemtable.ItemUrl = "~/Image/" + filename1;
filename1 = Path.Combine(Server.MapPath("~/Image/"), filename1);
itemtable.Imagefile1.SaveAs(filename1);
//if (ModelState.IsValid)
//{
// itemtable.ItemUrl = null;
db.Entry(itemtable).State = EntityState.Modified;
db.SaveChanges();
ModelState.Clear();
// return RedirectToAction("Index");
//}
return View(itemtable);
}
 
edit view page
 
@model onlineshopping_sonal.Models.Itemtable
@{
ViewBag.Title = "Edit";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Edit</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Itemtable</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.ItemId)
<div class="form-group">
@Html.LabelFor(model => model.CategoryId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("CategoryId", (IEnumerable<SelectListItem>)ViewBag.category, "-- Select --")
@Html.ValidationMessageFor(model => model.CategoryId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ProducerId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("ProducerId", (IEnumerable<SelectListItem>)ViewBag.product, "-- Select --")
@Html.ValidationMessageFor(model => model.ProducerId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Price, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Price, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Price, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ItemUrl, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<input type="file" name="Imagefile1" required />
@Html.ValidationMessageFor(model => model.ItemUrl, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
} ..
 
please share  edit  coded image full folder to us, regards 
 
 

Answers (1)