Note that there are some explanatory texts on larger screens.

plurals
  1. POint is null but TempData is not?
    primarykey
    data
    text
    <p>I need to pass some data between two action results so I thought I'd try <code>TempData</code> (I'm really new to MVC so bear with me). This is what I've tried:</p> <pre><code>public ActionResult Edit(Companies companies, HttpPostedFileBase file) { if (ModelState.IsValid) { try { if (file != null) { //Do stuff with file, save changes to database etc. TempData["Companies"] = companies; return RedirectToAction("bgcTest2", "Companies"); </code></pre> <p>Now the temp data is stored. And to retrieve it:</p> <pre><code>public ActionResult bgcTest2(string BolagsID, Companies companies) { try { companies = TempData["Companies"] as Companies; int test = companies.BolagsID; </code></pre> <p>Now here's the problem; <code>companies.BolagsID</code> has a value but <code>test</code> is always null. Why? And how do I fix it?</p> <p><strong>EDIT:</strong></p> <p>I've noticed a rather curious thing; when trying Darin Dimitrovs suggestion, and probably when doing my try aswell, if I put a breakpoint only on the line which returns the null value it doesn't break until the line after. As if that line is commented out and doesn't execute at all. Welcome to the Twilight Zone.</p> <p><strong>EDIT2:</strong></p> <p>Take 2:</p> <pre><code>[HttpPost] public ActionResult Edit(Companies companies, HttpPostedFileBase file) { if (ModelState.IsValid) { try { if (file != null) { // non related stuff return RedirectToAction("bgcTest2", "Companies", new {BolagsID = companies.BolagsID}); </code></pre> <p>And:</p> <pre><code>[Authorize] public ActionResult bgcTest2(int BolagsID) { try { int test = BolagsID; </code></pre> <p>I still get that "test does not exist in the current context".</p> <p><strong>EDIT3:</strong></p> <p>Take 2 works fine. My variables were <code>null</code> upon declaration because I never used them anywhere. I thought I'd take it one step at a time, but evidently, in some cases, you'll have to take a couple of steps to see it any of them works.</p> <p>Lesson learned: Be sure to use declared variables, otherwise they will be <code>null</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload