c# – 关于MVC 4视图中ViewData的Foreach
作者:互联网
我遇到的问题是我从我的数据库返回一张照片记录,这在我的详细信息视图中工作正常.但是在控制器下的细节我也正在编译一个列表对象,我想在我的详细信息视图中显示它,因为它包含相关的其他照片.因此,在视图的底部,它应列出每个引用的照片名称.我正在使用Galleria.Io照片查看器,我想将其插入其中.
当我尝试遍历列表中的每个项目时,我得到:
{"Object reference not set to an instance of an object."}
码
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;
}
细节视图
@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>
在索引视图中,我有:
@model IEnumerable<TestingMVCQA.Models.Photos>
这允许我使用foreach并且我理解为什么但是当我在详细信息视图中返回单个对象时,我对如何解决问题有点困惑.因此,当它全部工作时,我可以替换GetImage调用和foreach图像本身.
如果您需要任何其他代码或解释,请告诉我们.
解决方法:
您在foreach循环中使用ViewData [“NumberOfImages”]这是错误的.
你的foreach循环应如下所示: –
@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
}
标签:c,asp-net-mvc,html-helper 来源: https://codeday.me/bug/20190714/1459485.html