これはとても新しく、すべての仕組みを知るための小さなプロジェクトです。リストとモデルを使用したMVCエラー
だから私は_Layout.cshtmlファイル内のコードを配置し、私は、コードを上に壊れているViewBag.Title
に応じてどのような画像が表示制御しようとすると、各ページにヘッダー画像領域を作成するために探していますそのページの中にエラーの写真が続きます。私はちょうど問題が何であるかを理解することができません。
HomeController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace WebSite_Project_1.Controllers
{
public partial class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
[ActionName("Funny-Bones")]
public ActionResult FunnyBones()
{
ViewBag.Message = "This is funny bones.";
return View();
}
public class Headers
{
public string HeaderName { get; set; }
public string PageName { get; set; }
public int HeaderWidth { get; set; }
public int HeaderHeight { get; set; }
public string HeaderType { get; set; }
}
public ActionResult HeaderImages()
{
var model = new List<Headers>();
model.Add(new Headers { HeaderName = "home", PageName = "Home Page", HeaderWidth = 2200, HeaderHeight = 1172, HeaderType = ".png" });
model.Add(new Headers { HeaderName = "about", PageName = "About", HeaderWidth = 2200, HeaderHeight = 1172, HeaderType = ".png" });
model.Add(new Headers { HeaderName = "contact", PageName = "Contact", HeaderWidth = 2200, HeaderHeight = 1172, HeaderType = ".png" });
model.Add(new Headers { HeaderName = "funnybones", PageName = "Funny Bones", HeaderWidth = 2200, HeaderHeight = 1172, HeaderType = ".png" });
return View(model);
}
}
}
_Layout.cshtml
@model IEnumerable<WebSite_Project_1.Controllers.HomeController.Headers>
<div class="headersImage">
@foreach (var item in Model)
{
if (@item.PageName == @ViewBag.Title)
{
<img src="~/Content/Images/@[email protected]" title="@item.HeaderName" />
}
}
</div>
@RenderBody()
は、問題は、私がしようとすると起動し、それを公開してから、私は、このエラーは、foreachループでモデルを指します。
私は100%ループが正しいとは思っていませんが、まだそれを得ることはできませんでした。
この使用方法ではありませんモデルのフォルダをモデルフォルダに定義し、WebSite_Project_1.Modelsの名前空間 – hasan
をインポートしてListインスタンスを作成する必要があります。同じエラーを持つ他の人が検索エンジンを使用して見つけることができるように、 – Graham
この行をIEnumerableに変更します。ヘッダー>を作成し、HeaderImagesビューにあなたのforeachコードを書き込んで、NullExeptionを取得しないようにしてください。 –
hasan