Asp.net mvc 4 MVC 4,模型和图像同时出现
首先,我为我糟糕的英语道歉 我需要帮助。我试图深入研究ASP.NETMVC4,但一开始我就陷入了困境 假设我有两个模型:Asp.net mvc 4 MVC 4,模型和图像同时出现,asp.net-mvc-4,image-upload,Asp.net Mvc 4,Image Upload,首先,我为我糟糕的英语道歉 我需要帮助。我试图深入研究ASP.NETMVC4,但一开始我就陷入了困境 假设我有两个模型: public partial class Category { public Category() { this.Children = new HashSet<Category>(); this.Products = new HashSet<Product>(); } public in
public partial class Category
{
public Category()
{
this.Children = new HashSet<Category>();
this.Products = new HashSet<Product>();
}
public int ID { get; set; }
public string Name { get; set; }
public Nullable<int> ParentID { get; set; }
public Nullable<int> ImageID { get; set; }
public bool IsActive { get; set; }
public virtual ICollection<Category> Children { get; set; }
public virtual Category Parent { get; set; }
public virtual Image Image { get; set; }
public virtual ICollection<Product> Products { get; set; }
}
到目前为止还不错,但我还在编辑中。实现这一目标的最佳实践是什么?
当用户编辑类别时,他需要能够更改图像
我在互联网上四处寻找,但没有发现任何有用的东西
请帮忙。提前谢谢
编辑操作代码如下所示:
[HttpPost]
public ActionResult Edit(CategoryViewModel vmodel)
{
if (ModelState.IsValid)
{
var ctx = repo.Context;
{
ctx.Database.Connection.Open();
// begin transaction
var tran = ctx.Database.BeginTransaction();
try
{
// TODO: Add update logic here
// here i need to update the image before i update the category. Am i right?
repo.Update(vmodel.category);
repo.Save();
tran.Commit();
return RedirectToAction("index");
}
catch
{
tran.Rollback();
ViewBag.Error = "Error.";
}
}
}
// map model to viewmodel
vmodel.ParentSelectList = new SelectList(repo.FindAll(), "ID", "Name");
return View(vmodel);
}
你能显示你的编辑动作吗?如果可能的话,更新你的Createto来显示所有的代码。我更新了create动作,现在它完成了。还添加了编辑操作。在您的回购对象中,您没有更新图像的方法吗?哪个以model.FileName作为参数?如果没有,则需要针对repo.Updatemodel.Category创建它。你能展示一下这个方法的代码吗?从那里有人可以帮你创建一个通用的Imagerepo存储库。update方法只是将实体状态标记为已修改。公共虚拟void更新集实体{{u ctx.Entryentity.State=EntityState.Modified;}。我可以很容易地覆盖这个方法,我想你可以覆盖它的图像只是添加一些代码删除旧的图像,并保持其余的更新文件名
[HttpPost]
[ActionName("new")]
public ActionResult Create(CategoryViewModel model)
{
if (ModelState.IsValid)
{
var ctx = repo.Context;
{
ctx.Database.Connection.Open();
// begin transaction
var tran = ctx.Database.BeginTransaction();
try
{
// populate model
string save_path = null;
if (model.File != null)
{
string save_folder = Server.MapPath("~/App_Data/Uploads/Images/");
string image_name = Path.GetFileName(model.File.FileName);
string image_ext = Path.GetExtension(model.File.FileName);
image_name = image_name + new Guid().ToString() + image_ext;
save_path = model.category.Image.Path = Path.Combine(save_folder, image_name);
}
// save model
repo.Add(model.category);
repo.Save();
// save image
if(!String.IsNullOrEmpty(save_path))
model.File.SaveAs(save_path);
// commit transaction
tran.Commit();
// redirect
return RedirectToAction("index");
}
catch
{
tran.Rollback();
// add model state error
}
}
}
model.ParentSelectList = new SelectList(repo.FindAll(), "ID", "Name");
/*var list = repo.FindAll();
var selectList = new SelectList(list, "ID", "Name");
ViewBag.ParentSelectList = selectList;*/
return View(model);
}
[HttpPost]
public ActionResult Edit(CategoryViewModel vmodel)
{
if (ModelState.IsValid)
{
var ctx = repo.Context;
{
ctx.Database.Connection.Open();
// begin transaction
var tran = ctx.Database.BeginTransaction();
try
{
// TODO: Add update logic here
// here i need to update the image before i update the category. Am i right?
repo.Update(vmodel.category);
repo.Save();
tran.Commit();
return RedirectToAction("index");
}
catch
{
tran.Rollback();
ViewBag.Error = "Error.";
}
}
}
// map model to viewmodel
vmodel.ParentSelectList = new SelectList(repo.FindAll(), "ID", "Name");
return View(vmodel);
}