It might be an old question. I have recently created a website where the Navbar menus are dynamically generated by category. Inside those categories, different posts are created from the backend. Now the problem is when I change the name of the category, it is obvious that the <li> name in the website will also be updated. But it is not happening. Funny part is, it is changing in the database. I already checked that. When I am stopping my project and re-running it again, then it is working. Can anyone help me out with the problem?
N:B- I am taking my category name data through a global viewbag filter.
This is the code.
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
filterContext.Controller.ViewBag.WorkList = db.tbl_work.OrderByDescending(a=>a.work_id).ToList();
filterContext.Controller.ViewBag.WorkCategoryList = db.tbl_work_category.ToList();
filterContext.Controller.ViewBag.ServiceList = db.tbl_service.OrderByDescending(a => a.service_id).ToList();
filterContext.Controller.ViewBag.ServiceCategoryList = db.tbl_service_category.ToList();
filterContext.Controller.ViewBag.WorkMenuName = db.tbl_work.DistinctBy(a=>a.menu_id).ToList();
filterContext.Controller.ViewBag.ServiceMenuName = db.tbl_service.DistinctBy(a => a.menu_id).ToList();
filterContext.Controller.ViewBag.GalleryMenuName = db.tbl_gallery.DistinctBy(a => a.menu_id).ToList();
filterContext.Controller.ViewBag.ContactMenuName = db.tbl_contact.DistinctBy(a => a.menu_id).ToList();
}
Your implementation is not correct, OnResultExecuting
will not return updated data to you, that's because in the
OnResultExecuting
you are replacing the current result with a new instance. That will modify the result in the
ResultExecutingContext
but will leave the overall result unchanged.
Read some of the details on this
Similar Issue(not exactly same)
https://stackoverflow.com/questions/29693402/changing-filtercontext-result-in-onresultexecuting
My opinion to you would be, load dynamic menu by creating a partial View and then load data in usual Actionresult controller method and return a partial view in all the pages where you need a dynamic menu(probably it would be _layout.cshtml)
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly