SeventhWarhawk
SeventhWarhawk

Reputation: 1323

How to pass a variable from one ActionResult to another ActionResult? (ASP.NET MVC)

I want to be able to use the value of a string variable passed to one ActionResult, within another ActionResult.. How could i go about doing this?

public ActionResult PassCategoryPlaceHolder(string placeHolder)
        {

            var result = placeHolder;
            return RedirectToAction("EditCategory", result);
        }

I want to be able to use 'result' in my other ActionResult as follows:

public ActionResult EditCategory()
        {
            ViewBag.Message = Convert.ToString(result);
            return View();
        }

Note: I am trying to avoid sending 'placeHolder' directly to the ActionResult where i need it.

Upvotes: 0

Views: 1736

Answers (2)

MERLIN
MERLIN

Reputation: 61

Simply use a Session variable:

 public ActionResult PassCategoryPlaceHolder(string placeHolder)
            {

                Session["result"] = placeHolder;
                return RedirectToAction("EditCategory");
            }


    public ActionResult EditCategory()
            {
                var Message = Convert.ToString(Session["result"]);
                return View();
            }

Upvotes: 2

Ghanshyam Singh
Ghanshyam Singh

Reputation: 1381

You can do something like this

 public ActionResult PassCategoryPlaceHolder(string placeHolder)
    {
      var result = placeHolder;
      return RedirectToAction("EditCategory", new { message = result});
    }

And in other method:-

public ActionResult EditCategory(string message)
 {
   var model = new EditCategoryViewModel();
   model.Message = message;
   return View(model);
 }

Upvotes: 2

Related Questions