0

I feel really daft asking this, sure I'm missing something really simple, but I just can't get it to work!

I've created a ViewDataDictionary like this in the controller:

public ActionResult Index()
{
        var recipient = new Recipient() { 
            FullName = "John Brown",
            Company = "FP",
            Email = "name@website.com"
        };
        ViewData = new ViewDataDictionary(recipient);

        UserMailer.OnEventBook().Send();

        return View();
}

But I can't work out how to access the data in the view.

I've tried:

@ViewData["FullName"]

@ViewData.recipient["FulName"]

@ViewData[recipient.FullName]

@Model.FullName

@recipient.FullName

...and a number of similar combinations. Can anybody point me in the right direction?

Update

I realise I could just pass it through using ViewBag or ViewData["recipient"]. But I'm curious about this way of doing it.

The below is from the docs for MvcMailer, and the guy who wrote it seems to know his stuff, so I figured it must be a valid way of passing data through.

var comment = new Comment {From = me, To = you, Message = "Great Work!"};
ViewData = new ViewDataDictionary(comment);  

Update 2

Doh, I got it wrong.

The MvcMailer makes a call to an action within an action, and I should have been setting the ViewData in the sub action (OnEventBook() in the code above).

Martin Hansen Lennox
  • 2,837
  • 2
  • 23
  • 64

2 Answers2

3

First change your controller code to:

var recipient = new Recipient() { 
    FullName = "John Brown",
    Company = "FP",
    Email = "name@website.com"
};
ViewData["Recipient"] = recipient;

Then in you view use the following code:

@((Recipient)ViewData["Recipient"]).FullName

Anyway if you want a to use a specific object to the View use a Strongly-typed view.

Update

If you still want to use ViewDataDictionary you should use the following code to access the model in your view:

@ViewData.Model.FullName
ssimeonov
  • 1,416
  • 12
  • 13
  • Thanks for taking the time to answer. Is it possible to do it the other way as well? Just going to update the question with some details. – Martin Hansen Lennox Feb 19 '14 at 15:33
  • `@ViewData.Model.FullName` gives me a runtime binding on null reference exception. If I add a `@model` directive to the view I get a standard null ref exp. But I shouldn't need to because it's being passed as ViewData, is that right? – Martin Hansen Lennox Feb 19 '14 at 16:12
  • I've just double checked it and it's working. You'll have to update your question with the Controller and View code. – ssimeonov Feb 19 '14 at 16:20
  • Ok will do. Because it's using MvcMailer it's not a regular view call. If you would be kind as to check there's nothing obviously wrong... – Martin Hansen Lennox Feb 19 '14 at 16:29
  • I just realised what my error was. The action sending the mailer actually calls another action from within. (I updated the question to show it.) I should have been setting the ViewData inside of the OnEventBook() action instead of the Index() action. Thanks for your help, much appreciated :) – Martin Hansen Lennox Feb 19 '14 at 16:45
2

you can transfer the recipient data as "Model" from the controller to the view
CONTROLLERS:

public ActionResult Index()
{

   var recipient = new Recipient()
   {
       FullName = "John Brown",
       Company = "FP",
       Email = "name@website.com"
   };


 return view(recipient); //return the recipient as obj model
}

VIEWS:

@model YourNameSpace.YourRecipientModelFolder.Recipient
@{
  ViewBag.Title = "Home Index";
}
<div>Model:</div>
<div>@Model.FullName</div>
<div>@Model.Company</div>
<div>@Model.Email</div>

Note: YourNameSpace.YourRecipientModelFolder.Recipient is something like this MyMVC.Models.Recipient

cyan
  • 747
  • 5
  • 8
  • Thanks for your time to answer. I'm sorry I wasn't so clear in the original version of the question, I'm wondering if the way I posted was valid, and if so how to access it. – Martin Hansen Lennox Feb 19 '14 at 16:14