-1

In my MVC controller, I created an 'object' which I'm simplifying as 'student'. I'm using ViewBag to pass it on to my view.

In my controller, I have:

ViewBag.Student = new { 
    Id = "52", 
    Value = JsonConvert.SerializeObject(new
    {
         StudentName = oneStudent.Name,
         Age = oneStudent.Age,
         FavoriteColor = oneStudent.Color,
         Height = oneStudent.Height
    }),
    Text = oneStudent.Name + " --- " + oneStudent.Age,

};

I started off my calling the ViewBag, using

@{
    var student = ViewBag.Student;
}

and I want to use span (or anything similar) to display the data. My attempt was using some code along the lines of "@Student.Value" in the html code, but that didn't work.

Any help would be greatly appreciated. Thank you.

  • ViewBag is Dynamic then You can use Reflection or change your code to Dynamic student = ViewBag.Student; – Ahmed Galal Jan 20 '16 at 15:52
  • 4
    Using a view model in the view is easier than viewbag. – Andy Wiesendanger Jan 20 '16 at 15:53
  • @AhmedGalal Thanks for the suggestion, I'll look into it. – JoeFromAccounting Jan 20 '16 at 15:56
  • @AndyWiesendanger I can't use a model in this case, but I can see why it would be a lot more helpful. Just the way this page is set up in the project. – JoeFromAccounting Jan 20 '16 at 15:57
  • 1
    [This answer](http://stackoverflow.com/questions/8980045/storing-an-anonymous-object-in-viewbag) could provide you with more info about storing anonym type in viewbag. – Daniel Dušek Jan 20 '16 at 15:57
  • @WebAppStuff - I don't understand why you cannot use a model? Could you elaborate? If you already have a model on the page then i suggest creating a Container model (nothing more than a class with a property/field for each additional complex type you want to make use of in your view) which houses this model and the one that currently already exists on the page. This will give you type safety and also make the intent clear in both the controller and the view as to what the view is expecting. – Igor Jan 20 '16 at 16:24
  • 1
    The only reason to not use a model for this is if you *already* are passing something else as the model for the view. If that's the case, then you should be doing this in a child action or using a view model that encapsulates both this and your other model. Otherwise, *use a model*. – Chris Pratt Jan 20 '16 at 17:27

1 Answers1

2

You are setting an anonymous object to the Viewbag. You can not read the properties of that in your razor view.

What you should be doing is creating a view model to represent the data you want to send, create an object of that, initialize the property values and send to the view. Also it is not a good idea to use ViewBag to transfer data between your action method and your view. Since we will use a view model, we can make our view strongly typed to the view model and use that to transfer data.

public class StudentDetailVm
{
    public int Id { set; get; }
    public StudentVm Value { set; get; }   
}
public class StudentVm
{
    public string StudentName { set; get; }
    public int Age { set; get; }
    public int Height {set;get;}
}

And in your GET Action

public ActionResult Details(int id)
{
   var vm = new StudentDetailVm { Id = id,
                                 Value = new StudentVm { 
                                                        StudentName = "Scott", 
                                                        Age = 23}};
  return View(vm);
}

and in your view which is strongly typed to our view model

@model StudentDetailVm 
<h2>@Model.Id</h2>
<h3>@Model.Value.StudentName</h3>
Shyju
  • 214,206
  • 104
  • 411
  • 497