如何通过GET将“字符串”值从View传递给Controller

时间:2016-05-16 03:06:53

标签: c# asp.net-mvc razor

我需要使用 HttpGet !!将“字符串”值从View传递给Controller。

我执行以下操作,但它传递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 个答案:

答案 0 :(得分:2)

你可以修复它,例如:

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

答案 1 :(得分:0)

哦,刚想通了,我忘了设置参数类型。另外,为两种情况设置一个名称,以防万一。

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

现在它正常运作。