Argh! Why does System.Web.Mvc.HandleErrorInfo get passed to my views?

Here is an issue on codeplex explaining why that error occurs. Quote from http://web.archive.org/web/20131004122626/http://aspnet.codeplex.com/workitem/1795 since original link is dead: HandleError Attribute should not store exception information in ViewData When the HandleError attribute handles an exception, it stores the exception information in the ViewData. This is a problem when the Error.aspx inherits from the site.master and … Read more

ViewBag vs ViewData performance difference in MVC?

Okay – my initial answer basically said ‘no’ – time for a bit of a u-turn. It should be ‘no’ in a perfect dynamic world – but upon closer inspection it would appear that there will either be no difference (accounting for JIT magic) or it might be ever-so-slightly slower, although not enough to warrant … Read more

There is no ViewData item of type ‘IEnumerable’ that has the key ‘xxx’

Ok, so the answer was derived from some other posts about this problem and it is: If your ViewData contains a SelectList with the same name as your DropDownList i.e. “submarket_0”, the Html helper will automatically populate your DropDownList with that data if you don’t specify the 2nd parameter which in this case is the … Read more

How to set ViewBag properties for all Views without using a base class for Controllers?

The best way is using the ActionFilterAttribute. I’ll show you how to use it in .Net Core and .Net Framework. .Net Core 2.1 & 3.1 public class ViewBagActionFilter : ActionFilterAttribute { public ViewBagActionFilter(IOptions<Settings> settings){ //DI will inject what you need here } public override void OnResultExecuting(ResultExecutingContext context) { // for razor pages if (context.Controller is … Read more

Pass Additional ViewData to a Strongly-Typed Partial View

RenderPartial takes another parameter that is simply a ViewDataDictionary. You’re almost there, just call it like this: Html.RenderPartial( “ProductImageForm”, image, new ViewDataDictionary { { “index”, index } } ); Note that this will override the default ViewData that all your other Views have by default. If you are adding anything to ViewData, it will not … Read more

What’s the difference between ViewData and ViewBag?

It uses the C# 4.0 dynamic feature. It achieves the same goal as viewdata and should be avoided in favor of using strongly typed view models (the same way as viewdata should be avoided). So basically it replaces magic strings: ViewData[“Foo”] with magic properties: ViewBag.Foo for which you have no compile time safety. I continue … Read more

tech