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

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