2016-11-11 14 views
0

ASP MVCのビュー側からカスタム属性に値を渡そうとしています。 >Asp.net MVCでカスタム属性のパラメータを動的に渡す方法

  1. フィル従業員の詳細
  2. 保存ボタンをクリックが
  3. Passwordウィンドウが
  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メソッドのシグネチャでパスワードパラメータを再度指定する必要はありません。パラメータキーをハードコードしたくない場合は、PageNameおよびInvokeMethodのように、アクションキーをアクションフィルタクラスのプロパティに昇格します。 Btwでは、認証承認とASP .Netを使用して、FormsAuthenticationからASP .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

お返事ありがとう –

関連する問題