2016-05-16 83 views
0

我需要將「字符串」值從視圖傳遞到控制器使用HttpGet !!如何通過GET將「字符串」值從視圖傳遞到控制器

我這樣做,但它傳遞空:

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的值是「object htmlAttributes」,它不是參數。請參閱:http://screencast.com/t/93xgOe894b5 –

+0

也可以在地址欄中刪除?名稱? 我希望它像 'mysite.com/UserInfo/testuser'而不是'mysite.com/UserInfo?name = testuser /' –

0

哦,只是想通了,我忘了設定parametre的類型。另外,爲他們設置一個名字,以防萬一。

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

現在它工作正常。