0

I have the following code in my post action method for Edit.

            JobCardService.Update(viewData.JobCard);
            var js = new JavaScriptSerializer();
            ViewData["Notifications"] = js.Serialize(new {NoteificationType = "Success", Message = "The installtion was successfully updated"});
            return RedirectToAction("Index");

However, on the client, ViewData is null/empty, i.e. this client code

var notifications = eval("<%= ViewData["Notifications"]%>");

renders as

var notifications = eval("");

I'm sure I'm doing something small wrong.

ProfK
  • 49,207
  • 121
  • 399
  • 775
  • I'm confused, why do you have a redirectToAction in an Ajax call? – fearofawhackplanet Jul 16 '10 at 08:21
  • It's not an ajax call. I'm not using JSON for the whole response, I just want to add it to the response. – ProfK Jul 16 '10 at 08:43
  • eval needs 'padding' in order to work. try this instead: eval("(" + "<%= ViewData['Notifications']%>" + ")"); – jim tollan Jul 16 '10 at 09:12
  • note as well that you're not escaping your string correctly, you should have single quotes around anything contained in your double quotes. – jim tollan Jul 16 '10 at 09:18
  • No joy @jim, the js code is being rendered as: eval(''); It doesn't even get as far as a broken eval. There just isn't anything for it to even try eval'ing. – ProfK Jul 16 '10 at 09:32
  • It certainly is jim, but I have no idea what you mean by putting a model on the index view? I know I can set viewdata in the get action for the index, but how do I pass the notifications to it? – ProfK Jul 16 '10 at 09:45
  • one last throw - couldn't you put a model on the index view (if not already there) which had those attributes?? i know it doesn't address the issue directly but often pragmatism is key.. also, you could try using TempData instead of ViewData as the ViewData is linked to the edit controller and NOT the index one – jim tollan Jul 16 '10 at 09:47
  • ProfK - i was meaning a strongly typed model i.e. along these lines: <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Shareholder.Master" Inherits="System.Web.Mvc.ViewPage" %> – jim tollan Jul 16 '10 at 09:52

1 Answers1

0

ProfK - I think (as you'll no doubt be aware) you'll have to parse that json result in javascript once you get into your index view via the redirect. the jquery .getJson() method would seem most appropriate: http://api.jquery.com/jQuery.getJSON/

Also, as you're doing a RedirectToAction, then the context of the ViewData will be lost. In that case, you want to use TempData as a drop in replacement. Below is an example of what you could try:

jim

[edit] - not sure if this would work:

// in the controller
TempData["Notifications"] = js.Serialize(...);

// in the index view
function getMyJsondata() {
    var json = $.getJson('<%=ViewContext.TempData["Notifications"] %>');
}

or as per your amendment to the question, try this:

// alternative in index view
eval("(" + "<%= TempData['Notifications']%>" + ")"); 

give it a go...

adendum: to quote from a previous SO question on Tempdata vs ViewData: What is TempData collection used for in asp.net MVC?

TempData is used to share data between controller actions. If your controller does a RedirectToAction and the target action needs data (perhaps a particular model instance) to act upon, you can store this data in TempData. Using TempData is similar to storing it in the session, but only for one round-trip. You use TempData when you need to pass data to another controller action rather than a view for rendering.

Community
  • 1
  • 1
jim tollan
  • 22,305
  • 4
  • 49
  • 63
  • Thanks @jim, but my problem is now that that ViewData is simply not appearing on the client. – ProfK Jul 16 '10 at 09:13
  • ProfK - try using TempData (in the same way), rather than ViewData. TempData is session based, whereas ViewData is linked to that controller action (i.e. the Edit action) – jim tollan Jul 16 '10 at 09:48
  • Thanks, but TempData gave me the same issue. I have made master page typed for BaseViewData and moved my notifications collection to that class. I'll recheck TempData later, but for now things are wworking. – ProfK Jul 16 '10 at 11:51