2016-11-11 20 views
0

我试图将值传递给自定义属性从视图方在ASP MVC。我处理的流程是这样的 - >在Asp.net MVC我如何通过自定义属性中的参数动态

  1. 填充员工详细的保存按钮
  2. 点击
  3. 密码窗口已打开
  4. 这个密码,我想通过自定义属性(卡住这里)
  5. 条件将检查密码是否正确与否

下面是我的自定义属性

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); 
    } 
} 

下面是我的操作方法

[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(); 
} 

请朋友帮助我,如果我的观念是错误的,请让我知道。

回答

1

你不行。使用相同的ActionExecutingContext对象通过ActionParameters属性获取发布的值。您不必在Create方法签名中再次指定密码参数,除非您也需要它。如果您不想对参数键进行硬编码,请将其提升为动作过滤器类中的属性,就像您使用PageNameInvokeMethod所做的那样。顺便说一下,有很好的方法来做身份验证授权与ASP.Net,从FormsAuthenticationASP .Net Identity

public override void OnActionExecuting(ActionExecutingContext filterContext) 
{ 

    base.OnActionExecuting(filterContext); 

    var password = filterContext.ActionParameters.ContainsKey("password") 
       ? filterContext.ActionParameters["password"] : null; 
    .... 

} 
+0

换句话说,你*不能*传递密码,但你可以访问请求上下文并从那里获取值。 –

+0

是的......无论如何,我不认为OP会在这种情况下取​​得任何成功。客户是否应该在每次请求时都传递密码? –

+0

感谢您的回复 –

相关问题