2016-05-16 17 views
0

ViewからControllerへ 'string'値を渡す必要がありますHttpGet !!GETでViewからControllerへ 'string'値を渡す方法

私は次のようにしますが、それはnullを渡します。

View.cshtml

@Html.ActionLink("Profile", "Index", "UserInfo", new { name = User.Identity.Name }, new { hidefocus = "hidefocus" }) 

UserInfoController.cs

 [HttpGet] 
     public ActionResult Index(string user) 
     { 
      ... 
     } 
  • User.Identity.Name は私の文字列です。

答えて

2

次のようなそれを修正することができます:

@Html.ActionLink("Profile", "Index", "UserInfo", new { name = User.Identity.Name }, null) 
+0

ありがとうございます。どんなパラメタがゼロになっていますか? –

+0

値nullは「オブジェクトhtmlAttributes」です。パラメータではありません。参照してください:http://screencast.com/t/93xgOe894b5 –

+0

私はアドレスバーの名前を取り除くことができますか? 'mysite.com/UserInfo?name = testuser /' –

0

ああ、ちょうど私がparametreの種類を設定するのを忘れて、考え出しました。また、両方の場合に1つの名前を設定します。

UserInfoController.cs

 [HttpGet] 
     public ActionResult Index(string name) //check for area="" 
     { 
     ... 
     } 

View.cshtml

@Html.ActionLink("Profile", "Index", "UserInfo", new { name = (string)User.Identity.Name }, new { hidefocus = "hidefocus" }) 

今では正常に動作します。

関連する問題