2011-06-30 13 views
10

मैं एमवीसी एएसपीनेट पर काम कर रहा हूं।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); 
} 

मैं ingredientIndex कार्रवाई करने के लिए आईडी पास करना चाहते हैं। मैं यह कैसे कर सकता हूँ?

मैं इस आईडी सार्वजनिक एक्शन रिसैट घटक एडिट (int आईडी) का उपयोग करना चाहता हूं जो किसी अन्य पृष्ठ से आ रहा है। असल में मेरे पास दूसरी कार्रवाई में id नहीं है, कृपया मुझे सुझाव दें कि मुझे क्या करना चाहिए।

उत्तर

0

ऐसा क्यों नहीं करते?

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

सर मेरी संपादित प्रश्न देखें। –

+0

आपको प्रत्येक दृश्य के साथ आईडी को पास करना होगा –

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

अद्यतन

सबसे पहले मैं IngredientIndex और IngredientEdit नाम बदलने सिर्फ इंडेक्स और संपादित करें और IngredientsController में उन्हें जगह होता है, बजाय AdminController की, आप नामित व्यवस्थापक अगर आप चाहते हैं एक क्षेत्र हो सकता है।

// 
// 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

मैं [एचटीपीपोस्ट] कार्रवाई में "आईडी" के मूल्य का उपयोग कैसे कर सकता हूं। –

+0

ऊपर दिए गए उदाहरण को देखें। यदि आप अपना पांचवां आइटम (/ संपादित/5) संपादित कर रहे हैं, तो यह स्वचालित रूप से आईडी (int) पर मैप करेगा जब आप पोस्ट करेंगे (मानक मार्गों के साथ और इसी तरह ...)। –

0

इस तरह का प्रयास करें:

return RedirectToAction("IngredientIndex", new { id = productFormulation.id }); 
संबंधित मुद्दे