Action Parameter Naming

后端 未结 4 570
情书的邮戳
情书的邮戳 2020-12-25 11:18

Using the default route provided, I\'m forced to name my parameters \"id\". That\'s fine for a lot of my Controller Actions, but I want to use some better variable naming i

4条回答
  •  醉话见心
    2020-12-25 12:04

    Just because your route uses the name "id" for the ID variable doesn't mean that you have to use the same name in your controller action methods.

    For example, given this controller method...

    public Controller MailerController
    {
        public ActionResult Details(int mailerID)
        {
            ...
            return View(new { id = mailerID });
        }
    }
    

    ...and this action method call from the view...

    <%= Html.ActionLink("More Info", "Details", new { mailerID = 7 }) %>
    

    ...you can use whatever naming convention you wish for the ID parameter in your controller action methods. All you need to do is resolve the new name to the default, whether it's "id", "alias", or whatever.

    The above example should resolve to :

    More Info
    

提交回复
热议问题