Reputation: 645
I trying to pass value into custom attribute from view side in ASP MVC. My process flow will be like this -->
Below is my Custom Attributes
public class LevelSecurityAttribute : ActionFilterAttribute
{
public string PageName {get;set;}
public string Password { get; set; }
public string InvokeMethod { get; set; }
public void LevelSecurity(string pagename,string invokemethod,string password)
{
this.PageName = pagename;
this.Password = password;
}
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
//My Condition logic
base.OnActionExecuting(filterContext);
filterContext.Result=new RedirectResult("~/"+PageName+"/"+InvokeMethod);
}
}
Below is my Action Method
[HttpPost]
[ValidateAntiForgeryToken]
[LevelSecurity(PageName = "DocumentType", InvokeMethod = "Index", Password = **Need pass Password from View side stuck here**)]
public ActionResult Create(EmployeeMaster Employee,string password)
{
//Insert Employee Data if Password is Valid
return View();
}
Please friends help me and if my concept is wrong please let me know.
Upvotes: 0
Views: 2467
Reputation: 15683
You can't. Use the same ActionExecutingContext
object to grab the posted value via the ActionParameters
property. You don't have to specify the password parameter again in the Create method signature, unless you need it there too. If you don't want to hardcode the parameter key, promote it to a property in the action filter class, like you did with PageName
and InvokeMethod
. Btw, there are well established ways to do authentication and authorization with ASP .Net, from FormsAuthentication
to ASP .Net Identity
.
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
var password = filterContext.ActionParameters.ContainsKey("password")
? filterContext.ActionParameters["password"] : null;
....
}
Upvotes: 1