【问题标题】:Using ASP.Net MVC Razor - @Html.ActionLink使用 ASP.Net MVC Razor - @Html.ActionLink
【发布时间】:2018-01-14 04:43:02
【问题描述】:

我在 CustomerOrder 文件夹中的 .cshtml 中有一个 @Html.ActionLink,但我想指向不同文件夹中的操作方法。

我的操作链接是:

@Html.ActionLink("Order", "/Products/ProductList", new { id = Model.ID }, new { @class = "link-button btn-primary" })

我收到 HTTP 错误 404.0 - Not Found 错误,这是 URL:

http://localhost:63549/CustomerOrder/Products/ProductList/1

我希望得到的视图是:

http://localhost:63549/Products/ProductList/1

【问题讨论】:

    标签: asp.net-mvc


    【解决方案1】:

    您正在使用重载在同一参数中指定控制器和动作,该重载仅采用动作方法名称,在这种情况下不正确,因为您想在不同的控制器类上调用动作方法。

    您实际上需要使用以下重载方法指定操作方法名称和控制器名称:

    @Html.ActionLink("Order", "ProductList","Products", 
                     new { id = Model.ID }, 
                     new { @class = "link-button btn-primary" })
    

    在控制器类中,您必须为其添加一个操作方法:

    public class ProductsController : Controller
    {
    
         ................
         ................
         // other action methods here
    
         public ActionResult ProductsList(int id)
         {
            // your code goes here and finaly returns view
            // var model = new Model();
            return View(model)
         }
    
    }
    

    【讨论】:

    • 我有控制器类。但不是很明显的动作链接语法 - 我没有看到它......所以谢谢。
    猜你喜欢
    • 1970-01-01
    • 2011-06-03
    • 2011-12-04
    • 1970-01-01
    • 1970-01-01
    • 2012-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多