2

I haven't found another question that answers this directly. I have an Ajax.BeginForm on my View like so

@using (Ajax.BeginForm("Action", "Controller", new AjaxOptions())){
    //Stuff
    <div id="aaaa"> @TempData["Key"] </div>
}

In the action method I set a value of a message in TempData that I want to output.

TempData["Key"] = "Value";
return View("View");

When I debug it, I see that it successfully adds the value to TempData. I also see that it still has the value when it starts rendering the View again. But for reasons I cannot explain, it just outputs as

<div id="aaaa"> </div>

Why doesn't it output the value when I know it has it?

Same goes for ViewData and ViewBag and Model properties

tereško
  • 58,060
  • 25
  • 98
  • 150
quitstalin
  • 163
  • 1
  • 2
  • 18
  • is the value shown in the debug mode too in the view ? – Yasser Shaikh Mar 31 '12 at 07:11
  • Are you asking if it has the value when I put a breakpoint in the View? Yes, as I said in the question. It has the value when it renders the view, it hits the breakpoint to output the value, but it outputs an empty string. – quitstalin Mar 31 '12 at 16:12

1 Answers1

0

You seem to be using TempData and not ViewData which is not quite the same. Also you mentioned that you are using an Ajax.BeginForm to invoke this controller action. Since this is an AJAX call make sure that you have specified an UpdateTargetId in your AjaxOptions so that the resulting partial is injected somewhere into the DOM

here some related links http://davidhayden.com/blog/dave/archive/2009/05/19/ASPNETMVCAjaxBeginForm.aspx

ViewData not shown in Ajax.BeginForm

Community
  • 1
  • 1
Yasser Shaikh
  • 46,934
  • 46
  • 204
  • 281
  • As I said in the question, the same happens with ViewData and ViewBag. This is not a partialview. I can't set the UpdateTargetID, because it would inject the entire view into the view. I know because I messed around with those options and they don't make a difference with the problem. – quitstalin Apr 01 '12 at 05:34
  • Actually found the problem, MVC 3 is adding by default to the web.config. Atleast this is what I found when I was trying this out. By removing it, this works ! – Yasser Shaikh Apr 02 '12 at 05:54