JMK
JMK

Reputation: 28069

Accessing an object passed into a view

I am passing an object (a List) into a view like so:

private readonly CarsContext _db = new CarsContext();

public ActionResult Index()
{
    ViewBag.Message = "Foobar";

    var result = from cars in _db.CarProfiles
                 where cars.CarOfTheWeek
                 select cars;

    return View(result.ToList());
}

How do I access this object from within my Index view? I am thinking I need to add it to my dynamic viewbag, and access it that way instead of passing it as an argument, is this correct or if I keep my controller code above as it is, can I access this object from within the view?

Thanks

Upvotes: 0

Views: 1938

Answers (3)

Farshid Saberi
Farshid Saberi

Reputation: 947

You can access the object in your view by passing it in ViewBag ( The way that you have wrote your controller ). But I think, it is usually better to have a ViewModel and pass data to view, via that ViewModel. Passing Business objects directly to model ( the way you have done ), make model dependent to business object, that it is not optimal in the most cases. Also having ViewModel instead of using ViewBag has the advantage of using StrongTypes.

    private readonly CarsContext _db = new CarsContext();

    public ActionResult Index()
    {
        var result = from cars in _db.CarProfiles
                     where cars.CarOfTheWeek
                     select cars;

        MyViewModel myViewModel = new MyViewModel( );
        myViewModel.Message = "Foobar";
        myViewModel.ResultList = result.ToList();

        return View( myViewModel );
    }

Upvotes: 1

Shyju
Shyju

Reputation: 218882

Make your view strongly typed to the class/ type you are returning to, from your action method.

In your case you are returning a List of CarProfile class objects. So in your view(index.cshtml), drop this as the first line.

@model List<CarProfile>

Now you can access the list of CarProfile using the keyword Model.for example, you want to show all items in the list, in a loop, you can do like this

@model List<CarProfile>
@foreach(var item in CarProfile)
{
  <h2>@item.Name</h2>
}

Assuming Name is a property of CarProfile class.

Upvotes: 1

user1026130
user1026130

Reputation:

In your Index.cshtml view, your first line would be:

@model YOUR_OBJECT_TYPE

So for example, if you pass a string in your line:

return View("hello world");

Then your first line in Index.cshtml would be:

@model String

In your case, your first line in Index.cshtml would be:

@model List<CarProfile>

This way, if you type @Model in your Index.cshtml view, you could access to all the properties of your list of CarProfile. @Model[0] would return your first CarProfile in your list.

Just a tip, the ViewBag shouldn't be used most of the time. Instead, you should create a ViewModel and return it to your view. If you want to read about ViewModels, here's a link: http://msdn.microsoft.com/en-us/vs2010trainingcourse_aspnetmvc3fundamentals_topic7.aspx

Upvotes: 1

Related Questions