Tuesday, August 23, 2011

TempData

I used TempData to solve this problem.

public static string tempVariableName = "Whatever";

   

public ActionResult Edit(Guid id)

{

   var fooModel = GetTheModelSomehow(id);

   return View("Create", fooModel);

}

   

[HttpPost]

public ActionResult Edit(FooModel fooModel, Guid id)

{

   SaveTheModelSomehow(fooModel);

   TempData[tempVariableName] = id.ToString();

   return RedirectToAction("Details");

}

   

public ActionResult Details(string id)

{

   if (TempData[tempVariableName] != null && id.IsNullOrEmpty())

   {

      id = TempData[tempVariableName] as string;

   }

   Guid guidKeyForMatching = new Guid(id);

   var fooModel = GetTheModelSomehow(guidKeyForMatching);

   return View(fooModel);

}

 
 

Imagine, if you will, that the above code is a piece of a controller that governs Foo. One may enter screens for just viewing non-editable details on an existing Foo or for actually editing an existing Foo by way of passing a Guid in the URL line. When one submits a form at the Create view the form reposts to the same URL taking one to the middle action above which in turn, after updating the Foo in question, takes one back to non-editable details.

No comments:

Post a Comment