Asp.Net MVC call method when every view is called - asp.net-mvc

I have some ViewBag need to pass to every views when them are called.
For example:
public ActionResult Index()
{
ViewBag.Importan1 = "A";
ViewBag.Importan2 = "B";
return View();
}
public ActionResult Detail()
{
ViewBag.Importan1 = "A";
ViewBag.Importan2 = "B";
return View();
}
and I want to:
public ActionResult Index()
{
return View();
}
public ActionResult Detail()
{
return View();
}
but the ViewBag Important1 and Important2 are called implicit.

I would suggest you a custom filter attribute. You create it (here some good examples http://msdn.microsoft.com/en-us/library/dd381609(v=vs.100).aspx) and do all the work inside. Then you just have to apply the filter on your controller and it will affect all your action methods.

You can write a super class for the controller defines Index and Detail as virtual (to be able to override them and add some extra functionality) and set Important1 and Important2 in your base class as
public class MyController : Controller
{
public virtual ActionResult Index()
{
ViewBag.Importan1 = "A";
ViewBag.Importan2 = "B";
return View();
}
public virtual ActionResult Detail()
{
ViewBag.Importan1 = "A";
ViewBag.Importan2 = "B";
return View();
}
}
that is it, you have Index and Detail with your desired behavior in all of your controllers which is inherited from MyController from example
public class Controller1 : MyController
{
public override ActionResult Index()
{
var result = base.Index();
//do some manipulation here
return result;
}
}
Another Solution
You can write this controller (I didn't check that on my pc)
public class MyController : Controller
{
protected override void OnActionExecuting(
ActionExecutingContext filterContext)
{
//write your custom code here
}
}
then inherit your controller from MyController
Another Solution
using filterActionAttribute and write your own filer action attribute

Related

how to check condition if temp data has value in every controller

I am looking for the optimized way of checking if tempdata has value or not in every controller in MVC. I don't want to write in every controller, Where can I write this condition that if temp data has value then show the view else redirect to Login page.
Please create a base controller
public class BaseController : Controller
{
protected override void OnActionExecuting(System.Web.Mvc.ActionExecutingContext filterContext)
{
var id = filterContext.Controller.TempData["id"];
if (id == null)
{
filterContext.Result = RedirectToAction("Index", "Home");
}
base.OnActionExecuting(filterContext);
}
}
After that you have to inherit the base controller on very controller.
public class CheckController : BaseController
{
public ActionResult Index()
{
TempData["id"] = 2;
return View();
}
}
Use base controller in every controller. Then check in base controller that if temp data has value then show the view else redirect to Login page.
Filter code:
public class MyCustomAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
filterContext.Controller.TempData.Add("some key", "some value");
}
}
Action code:
[MyCustom]
public ViewResult Index()
{
string value = TempData["key"] as string;
return View();
}

How to find view name for action method

For instance this action
public class UserController : Controller
{
public ActionResult SomeAction()
{
// some code here
return View();
}
}
How do we know the view name for the SomeAction()?

Redirect to same view

I am working on a ASP.NET MVC website and I am new to this.
I have a controller with few actions. I want to use these actions through out my website.
For example
[HttpPost]
public ActionResult MyAction(ViewModel model)
{
if (ModelState.IsValid)
{
//code is here
}
return RedirectToAction(); // redirect to same view
}
I want to redirect to same view from where request is generated. I am not sure if this is possible or not ?
Based on your comment, I would create a Controller that looks like:
public MyController : Controller
{
private ActionResult SharedMethod(SomeModel model)
{
if (ModelState.IsValid)
{
//code is here
}
// viewname is required, otherwise the view name used will be
// the original calling method (ie public1.cshtml, public2.cshtml)
return this.View("SharedViewName");
}
public ActionResult Public1(SomeModel model)
{
return this.SharedMethod(model);
}
public ActionResult Public1(SomeModel model)
{
return this.SharedMethod(model);
}
}

assigning a layout for an entire controller

is there any way to assign an entire controller to use a certain layout? I know you can assign the layout in the ViewStart, is there anyway for the viewStart to know what controller is being used?
My objective is to have two admin layouts, one with an extra navbar when you are working with anything in the admin controller.
You could write a custom action filter:
public class LayoutAttribute : ActionFilterAttribute
{
private readonly string _layout;
public LayoutAttribute(string layout)
{
_layout = layout;
}
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
var result = filterContext.Result as ViewResult;
if (result != null)
{
result.MasterName = _layout;
}
}
}
and then decorate your controller with it and all actions (returning view results obviously) withing this controller will use the layout you have specified:
[Layout("_SimpleLayout")]
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult About()
{
return View();
}
}

Accessing ASP.NET MVC Action Parameters

This must be simple, but I can't seem to figure it out. I am setting an action parameter inside an action filter as follows:
public class MyFilter : ActionFilterAttribute
{
public override void OnActionExecuting (ActionExecutingContext filterContext)
{
filterContext.ActionParameters["MyParam"] = "MyValue";
}
}
I am applying the filter to an entire controller as follows:
[MyFilter]
public class HomeController : Controller
{
public ActionResult Index()
{
// How do I access MyParam here?
return View();
}
}
}
How do I access MyParam inside an action method?
Maybe you could use:
[MyFilter]
public ActionResult Index(string MyParam)
{
//Do something with MyParam
return View();
}
You can decorate whole controller with [MyFilter] or only one action.
I'm hoping this will work:
var myParam = ValueProvider.GetValue("MyParam").RawValue as string;
Since ValueProvider is what modelbinders use to get the values I would think it should be able to get the value set in your filter.

Resources