gpt4 book ai didi

asp.net-mvc - 如何在 RedirectToAction 中传递参数?

转载 作者:行者123 更新时间:2023-12-01 19:31:55 25 4
gpt4 key购买 nike

我正在开发 MVC asp.net。

这是我的 Controller 操作:

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 public ActionResult做什么成分编辑(int id)。实际上我在第二个操作中没有id,请建议我该怎么办。

最佳答案

return RedirectToAction("IngredientIndex", new { id = id });

更新

首先,我将 IngredientIndex 和 IngredientEdit 重命名为 Index 和 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)
}

关于asp.net-mvc - 如何在 RedirectToAction 中传递参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6531190/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com