简体   繁体   English

如何在Url.Action中传递一些参数?

[英]How to pass some parameters in Url.Action?

Url.Action("CreatePerson", "Person", new { id = 6, name = "rachel", grade="a" });

When I get the parameters in the action, I get only the first value (value of id ), but I don't get the values of name and grade . 当我在操作中获取参数时,仅获取第一个值( id值),但没有获取namegrade的值。 Why? 为什么?

You need to define the additional parameters on the controller method like so: 您需要像这样在控制器方法上定义其他参数:

 public ActionResult SuccessfulPostData(int id, string name, string grade)
        {

            ViewBag.name= name;
            return View();
        }

In the View you can display the value: 在视图中,您可以显示值:

 @Viewbag.name

Also sometimes you need to explicitly define route parameters. 同样,有时您需要显式定义路由参数。 This is found in the RouteConfig.cs file. 在RouteConfig.cs文件中找到此文件。

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}/{name}/{grade}",
        defaults: new { controller = "Home", action = "SuccessfulPostData", id = UrlParameter.Optional, name = UrlParameter.Optional, grade = UrlParameter.Optional }
    );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM