Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Foreach on ViewData in MVC 4 View

The problem I have is I'm returning a single photo record from my database, which is working fine in my details view. However in the controller under details I'm also compiling a list object, which I want to display in my details view as this contains additional photos that are associated. So at the bottom of the view it should list each referenced photo name. I'm using the Galleria.Io photo viewer and I want to plug this into it.

When I try and loop through each item in the list I'm getting:

{"Object reference not set to an instance of an object."}

Code

PhotosController

public ActionResult Details(int id = 0)
{
    Photos photos = db.FindPhotoById(id);
    if (photos == null)
    {
        return HttpNotFound();
    }

    List<Photos> photoList = GetImagesFromFilmID(8);

    ViewData["NumberOfImages"] = photoList.Count().ToString();
    ViewData["PhotoList"] = photoList;

    return View(photos);
}

public List<Photos> GetImagesFromFilmID(int filmID = 8 )
{

    List<Photos> photos;

    photos = (from p in db.Photos
              where p.PhotoDescription == "8"
              select p).Take(filmID).ToList();

    return photos;
}

Details View

@model TestingMVCQA.Models.Photos
@*@model IEnumerable<TestingMVCQA.Models.Photos>*@
@{
    ViewBag.Title = "Details";
}

<h2>Details</h2>

<fieldset>
<legend>Photos</legend>

<div class="display-label">
    @Html.DisplayNameFor(model => model.PhotoName)
</div>
<div class="display-field">
    @Html.DisplayFor(model => model.PhotoName)
</div>

<div class="display-label">
    @Html.DisplayNameFor(model => model.PhotoDescription)
</div>
<div class="display-field">
    @Html.DisplayFor(model => model.PhotoDescription)
</div>

<div class="display-label">
    @Html.DisplayNameFor(model => model.PhotoFileName)
</div>
<div class="display-field">
    @Html.DisplayFor(model => model.PhotoFileName)
</div>

<div class="display-label">
    @Html.DisplayNameFor(model => model.ImageMimeType)
</div>
<div class="display-field">
    @Html.DisplayFor(model => model.ImageMimeType)
</div>

<script src="@Url.Content("~/Scripts/jquery-1.7.1.js")"></script>
<script src="@Url.Content("~/Scripts/galleria-1.3.6.js")"></script>

<style>
   .galleria{ width: 900px; height: 500px; background: #000 }
</style>

@if (Model.PhotoFile != null) {
  <div class="galleria">
     <img src="@Url.Action("GetImage", "Photos", new { id = Model.PhotoID })" data-title="My title" data-description="My description" />
<img src="@Url.Action("GetImage", "Photos", new { id = 5 })" />
<img src="@Url.Action("GetImage", "Photos", new { id = 6 })" />
<img src="@Url.Action("GetImage", "Photos", new { id = 7 })" />
<img src="@Url.Action("GetImage", "Photos", new { id = 8 })" />
<img src="@Url.Action("GetImage", "Photos", new { id = 9 })" />
<img src="@Url.Action("GetImage", "Photos", new { id = 10 })" />
<img src="@Url.Action("GetImage", "Photos", new { id = 11 })" />
<img src="@Url.Action("GetImage", "Photos", new { id = 12 })" />
<img src="@Url.Action("GetImage", "Photos", new { id = 13 })" />
<img src="@Url.Action("GetImage", "Photos", new { id = 14 })" />
</div>
}

<script>
    Galleria.loadTheme(src = "@Url.Content("~/Scripts/galleria.classic.js")");
    Galleria.run('.galleria');
</script>

</fieldset>

<p>Your favorite fruit is:</p>@ViewData["NumberOfImages"]
<p>
   @foreach (var item in ViewData["NumberOfImages"] as IEnumerable<TestingMVCQA.Models.Photos>)
{
}
</p>

<p>
@Html.ActionLink("Edit", "Edit", new { id=Model.PhotoID }) |
@Html.ActionLink("Back to List", "Index")
</p>

In the index view I have:

@model IEnumerable<TestingMVCQA.Models.Photos>

Which allows me to use the foreach and I understand why but as I'm returning a single object in the details view I'm a bit stuck on how to resolve the issue. So when it is all working I can then replace the GetImage call and foreach on the image itself.

If you require any additional code or explanation then let me know.

like image 776
user2672720 Avatar asked Jul 27 '14 14:07

user2672720


People also ask

How do I pass ViewData in view?

To pass the strongly-typed data from Controller to View using ViewData, we have to make a model class then populate its properties with some data and then pass that data to ViewData dictionary as Value and selecting Key's name is the programmer's choice.

Can we pass data from view to controller using ViewData?

Summary. ViewData, ViewBag, and TempData are used to pass data between controller, action, and views. To pass data from the controller to view, either ViewData or ViewBag can be used. To pass data from one controller to another controller, TempData can be used.

What is ViewData in MVC with example?

ViewData is a dictionary of objects that are stored and retrieved using strings as keys. It is used to transfer data from Controller to View. Since ViewData is a dictionary, it contains key-value pairs where each key must be a string. ViewData only transfers data from controller to view, not vice-versa.


1 Answers

You are using ViewData["NumberOfImages"] in foreach loop which is wrong.

Your foreach loop should look like this :-

@foreach (var item in ViewData["PhotoList"] as IEnumerable<TestingMVCQA.Models.Photos>)
{
  //@item.FileName //instead .FileName use property which you have used in your model class
}
like image 142
Kartikeya Khosla Avatar answered Oct 04 '22 03:10

Kartikeya Khosla