Html.DisplayFor throws Nullreference Exception

Mar 21, 2012 at 2:46 PM
Edited Mar 21, 2012 at 2:48 PM

Hi!

I am testing av view that calls the following partial view:

@Html.DisplayFor(m => webImage, new { Caption = Model.Title }) Throws NullReferenceExeption:
at System.Web.Mvc.Html.TemplateHelpers.GetActionCache(HtmlHelper html)   
at System.Web.Mvc.Html.TemplateHelpers.ExecuteTemplate(HtmlHelper html, ViewDataDictionary viewData, String templateName, DataBoundControlMode mode, GetViewNamesDelegate getViewNames, GetDefaultActionsDelegate getDefaultActions)   
at System.Web.Mvc.Html.TemplateHelpers.TemplateHelper(HtmlHelper html, ModelMetadata metadata, String htmlFieldName, String templateName, DataBoundControlMode mode, Object additionalViewData, ExecuteTemplateDelegate executeTemplate)   
at System.Web.Mvc.Html.TemplateHelpers.TemplateHelper(HtmlHelper html, ModelMetadata metadata, String htmlFieldName, String templateName, DataBoundControlMode mode, Object additionalViewData)   
at System.Web.Mvc.Html.TemplateHelpers.TemplateFor[TContainer,TValue](HtmlHelper`1 html, Expression`1 expression, String templateName, String htmlFieldName, DataBoundControlMode mode, Object additionalViewData, TemplateHelperDelegate templateHelper)   
at System.Web.Mvc.Html.TemplateHelpers.TemplateFor[TContainer,TValue](HtmlHelper`1 html, Expression`1 expression, String templateName, String htmlFieldName, DataBoundControlMode mode, Object additionalViewData)   
at System.Web.Mvc.Html.DisplayExtensions.DisplayFor[TModel,TValue](HtmlHelper`1 html, Expression`1 expression, Object additionalViewData)   
at Nrk.Programspiller.Frontend.Views.Shared.DisplayTemplates.ProgramEpgEntry.Execute() 

When debugging it looks like the Model property for the HTML helper is NULL..


My Test code:

  var url = "http://testurl/";

var requestMock = new Mock<HttpRequestBase>();
requestMock.SetupGet(o => o.Url).Returns(new Uri(url));
requestMock.SetupGet(o => o.QueryString).Returns(new NameValueCollection());
var contextMock = new Mock<HttpContextBase>();
context;pcl.SetupGet(x => x.Request).Returns(requestMock.Object);


_epgEntry = new ProgramEpgEntry
{
	RelativeTimeType = RelativeTimeEnum.Present,
	Title = "TestTitle",
	Duration = TimeSpan.FromHours(1),
	PlannedStartTime = DateTime.Now,
	ActualStartTime = DateTime.Now,
	Image = new WebImage

	{
		ImageUrl = "imageUrl",
		PixelWidth = 172,

		CdnImage = new CdnImage
		{ 
			ImageIdWithCropInfo =string.Empty, 
			WebImages = new List<WebImage>()
		}
	}					



};
_programEpgEntryView.ViewBag.IsLivePage = true;
_programEpgEntryView.ViewBag.CssClass = "cssClass";
_programEpgEntryView.ViewBag.Caption = "caption";

HtmlDocument doc = _programEpgEntryView.RenderAsHtml(contextMock, _epgEntry);

Any ideas what is wrong?