2016-05-16 37 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" }) 

现在它工作正常。

相关问题