我正在研究MVC asp.net。
这是我的控制器动作:
public ActionResult ingredientEdit(int id) {
ProductFormulation productFormulation = db.ProductFormulation.Single(m => m.ID == id);
return View(productFormulation);
}
//
// POST: /Admin/Edit/5
[HttpPost]
public ActionResult ingredientEdit(ProductFormulation productFormulation) {
productFormulation.CreatedBy = "Admin";
productFormulation.CreatedOn = DateTime.Now;
productFormulation.ModifiedBy = "Admin";
productFormulation.ModifiedOn = DateTime.Now;
productFormulation.IsDeleted = false;
productFormulation.UserIP = Request.ServerVariables["REMOTE_ADDR"];
if (ModelState.IsValid) {
db.ProductFormulation.Attach(productFormulation);
db.ObjectStateManager.ChangeObjectState(productFormulation, EntityState.Modified);
db.SaveChanges();
**return RedirectToAction("ingredientIndex");**
}
return View(productFormulation);
}
我想将id传递给ingredientIndex
动作。我怎么能这样做?
我想使用来自另一个页面的id 公共ActionResult ingredientEdit(int id)。实际上我在第二次行动中没有id
,请告诉我该怎么做。
答案 0 :(得分:26)
return RedirectToAction("IngredientIndex", new { id = id });
更新
首先我将IngredientIndex和IngredientEdit重命名为Index and Edit并将它们放在IngredientsController中,而不是AdminController,如果需要,可以有一个名为Admin的区域。
//
// GET: /Admin/Ingredients/Edit/5
public ActionResult Edit(int id)
{
// Pass content to view.
return View(yourObjectOrViewModel);
}
//
// POST: /Admin/Ingredients/Edit/5
[HttpPost]
public ActionResult Edit(int id, ProductFormulation productFormulation)
{
if(ModelState.IsValid()) {
// Do stuff here, like saving to database.
return RedirectToAction("Index", new { id = id });
}
// Not valid, show content again.
return View(yourObjectOrViewModel)
}
答案 1 :(得分:0)
为什么不这样做?
return RedirectToAction("ingredientIndex?Id=" + id);
答案 2 :(得分:0)
尝试这种方式:
return RedirectToAction("IngredientIndex", new { id = productFormulation.id });