简体   繁体   English

在MVC视图中创建ActionLink

[英]Creating ActionLink in MVC View

I'm fairly new to MVC so, please excuse my possibly incorrect use of terminology. 我是MVC的新手,所以请原谅我可能不正确使用术语。

In the RouteConfig.cs file of my MVC app there is this routes.MapRoute :- 在我的MVC应用程序的RouteConfig.cs文件中,有以下路由。

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

The key line being :- 关键是:

url: "{controller}/{action}/{id}"

so, I followed the example of creating ActionLinks, that were created in my default app template, and cobbled together this line :- 因此,我遵循了在默认应用程序模板中创建的创建ActionLink的示例,并将以下行拼凑在一起:-

<p>@Html.ActionLink("portfolio details", "Detail", "Portfolio", new { portfolioId = portfolio.PortfolioId } , new { @class = "btn btn-default" })</p>

However, this gives me the URL :- 但是,这给了我URL:-

http://localhost:xxxxx/Portfolio/Detail?portfolioId=174198

If, I don't want the ID to be in a QueryString parameter, how do I create the link to match the pattern that is expected in the routes.MapRoute so that I get a URL link such as :- ? 如果我不希望ID在QueryString参数中,那么如何创建链接以匹配route.MapRoute中期望的模式,以便获得URL链接,例如:-?

http://localhost:xxxxx/Portfolio/Detail/174198 HTTP://本地主机:XXXXX /组合/详细信息/ 174198

你可以用这种方式

<p>@Html.ActionLink("portfolio details", string.Format("Detail/{0}", portfolio.PortfolioId ), "Portfolio", null, null)</p>

Assuming your controller action has the parameter id then you could do: 假设您的控制器操作具有参数id则可以执行以下操作:

<p>@Html.ActionLink("portfolio details", "Detail", "Portfolio", new { id = portfolio.PortfolioId } , new { @class = "btn btn-default" })</p>

This should give you: 这应该给您:

http://localhost:xxxxx/Portfolio/Detail/174198

The this would pass 174198 to the controller action as the id parameter. 这会将174198作为id参数传递给控制器​​动作。

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

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