如何通过GET将“字符串”值从视图传递到控制器
问题描述:
我需要将“字符串”值从视图传递到控制器使用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 是我的字符串。
答
你能解决这个问题,如:
@Html.ActionLink("Profile", "Index", "UserInfo", new { name = User.Identity.Name }, null)
答
哦,只是想通了,我忘了设定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" })
现在它工作正常。
谢谢。什么参数被禁止? –
null的值是“object htmlAttributes”,它不是参数。请参阅:http://screencast.com/t/93xgOe894b5 –
也可以在地址栏中删除?名称? 我希望它像 'mysite.com/UserInfo/testuser'而不是'mysite.com/UserInfo?name = testuser /' –