I'm trying to crate a custom action filter attribute. And some where, I need facilities, such us TempData[key] and TryUpdateModel... My custom attribute class deriving from the ActionFilterAttribute, I can access both below methods.
public override void OnActionExecuting(ActionExecutingContext filterContext) { } public override void OnResultExecuted(ResultExecutedContext filterContext) { }
Unfortunately, from both filtercontext local variables, I don't know how to access the TempData. I've tried to follow several leads, but without success. After all, maybe there's TempData in the filterContext variables. In that case, how do I access the TemData available?
Thanks for helping
TempData is used to transfer data from view to controller, controller to view, or from one action method to another action method of the same or a different controller. TempData stores the data temporarily and automatically removes it after retrieving a value. TempData is a property in the ControllerBase class.
TempData is created on Server Side of the Web application and hence it is not possible to directly set it on Client Side using JavaScript or jQuery. Thus, only possible way is to set it by making an AJAX call to the Controller's Action method using jQuery AJAX function in ASP.Net MVC Razor.
Passing the data from Controller to View using TempDataGo to File then New and select “Project” option. Then create the ASP.NET web application project as depicted below. Then select “Empty” and tick “MVC” then click OK.
TempData is a dictionary object derived from TempDataDictionary, which can contain key-value pairs, useful for transferring data from controller to view in ASP.NET MVC Application, TempData stays for a subsequent HTTP Request as opposed to other options ( ViewBag and Viewdata ) those stay only for current request.
var foo = filterContext.Controller.TempData["foo"];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With