2011-06-30 13 views
10

私はMVC asp.netで作業しています。私はingredientIndexアクションにIDを渡したいRedirectToActionでパラメータを渡す方法はありますか?

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を使用したい他のページから来ているpublic ActionResult ingredientEdit(int id)です。実際には私はidを第二のアクションで持っていないので、どうすればいいのか教えてください。

答えて

0

どうすればよいですか?

return RedirectToAction("ingredientIndex?Id=" + id); 
+0

卿、私の編集した質問をご確認ください。 –

+0

すべてのビューにidを渡す必要があります –

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

更新

まず私の代わりにAdminControllerの、あなたが欲しい場合は管理者という名前のエリアを持つことができ、単にインデックスと編集にIngredientIndexとIngredientEditの名前を変更し、IngredientsControllerに配置します。

// 
// 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) 
} 
+2

[HttpPost]アクションで "id"の値を使用するにはどうすればいいですか? –

+0

上記の例を見てください。 5番目のアイテム(/ Edit/5)を編集している場合、POST(標準ルートなど)で自動的にid(int)にマップされます。 –

0

この方法を試してみてください:

return RedirectToAction("IngredientIndex", new { id = productFormulation.id }); 
関連する問題