- 爱易网页
-
ASP.NET教程
- MVC3.0 修改商品提交时出错。该如何处理
日期:2014-05-17 浏览次数:20488 次
MVC3.0 修改商品提交时出错。
刚开始学,照着书上弄的,不知道为什么出错,大家帮帮我。
控制器敌源码:
public ActionResult ProductList()
{
List<Product> products = (from p in eshop.Product
orderby p.ProductID descending
select p).ToList();
return View(products);
}
public ActionResult AddProduct()
{
return View();
}
public ActionResult SaveProduct()
{
Product product = new Product();
product.Name = Request.Form["Name"];
product.Description = Request.Form["Description"];
product.Price = Convert.ToDecimal(Request.Form["Price"]);
string savePath = HttpContext.Request.MapPath(@"/content/images/");
product.ImageUrl = SaveFile(HttpContext.Request.Files[0], savePath);
eshop.AddToProduct(product);
eshop.SaveChanges();
return RedirectToAction("ProductList");
}
public ActionResult EditProduct(int id)
{
Product product = (from p in eshop.Product
where p.ProductID == id
select p).First();
if (Request.RequestType == "POST")
{
product.Name = Request.Form["Name"];
product.Description = Request.Form["Description"];
product.Price = Convert.ToDecimal(Request.Form["Price"]);
string savePath = HttpContext.Request.MapPath(@"/content/images/");
product.ImageUrl = SaveFile(HttpContext.Request.Files[0], savePath);
eshop.AddToProduct(product);
eshop.SaveChanges();
return RedirectToAction("ProductList");
}
return View(product);
}
视图(View):
<h2>修改商品信息</h2>
<form action="../EditProduct/<%=ViewData.Model.ProductID %>" method="post" enctype="multipart/form-data">
<p>商品名称:<%=Html.TextBox("Name") %></p>
<p>商品描述:<%=Html.TextBox("Description") %></p>
<p>商品价格:<%=Html.TextBox("Price") %></p>
<p>选择图片:<input type="file" name="ImageUrl" /></p>
<p>
<input type="submit" value="修 改" />
<input type="button" value="取 消" />
</p>
</form>
新加产商品时就可以,就是修改不行,帮忙看看为什么?
------解决方案--------------------
MVC我只懂皮毛,看你edit的函数中,有 eshop.AddToProduct(product); 在我看来,这个不应该出现。
------解决方案--------------------
if (Request.RequestType == "POST")
{
product.Name = Request.Form["Name"];
product.Description = Request.Form["Description"];
product.Price = Convert.ToDecimal(Request.Form["Price"]);
string savePath = HttpContext.Request.MapPath(@"/content/images/");