需要传递参数和css使用Html.Actionlink

Need to pass parameters and css using Html.Actionlink

我正在尝试使用 ActionLink 调用控制器方法。我可以访问控制器方法,甚至使用 css 使 ActionLink 看起来像它应该的按钮,但我无法传递参数 - 每次我尝试时,我要么得到错误,要么值发生变化为空。

这些实际上会到达方法并到达我设置的断点,但没有任何参数值。

<p>@Html.ActionLink("Submit Request", "Create", "HomeController",  new { @class = "btn btn-primary"})</p>

<p>@Html.ActionLink("Submit Request", "Create", "HomeController",  new { @class = "btn btn-primary", title = "hello", description = "world"})</p>

单击按钮后立即出现错误,特别是 /HomeController/Create 的 HTTP 404 错误。

<p>@Html.ActionLink("Submit Request", "Create", "HomeController", new { title = "hello", description = "world" }, new { @class = "btn btn-primary"})</p>

<p>@Html.ActionLink("Submit Request", "Create", "HomeController", new { @class = "btn btn-primary"}, new { title = "hello", description = "world" })</p>

为了完整起见,这里是 HomeController 中的方法

    public ActionResult Create(string title, string description)
    {
        try
        {
            return RedirectToAction("Index");
        }
        catch (Exception ex)
        {
            return View("Index2");
        }
    }

谁能告诉我我遗漏了什么或做错了什么?

您的第一次尝试是使用此版本的重载

@Html.ActionLink(linkText: "Submit Request",
                 actionName: "Create",
                 routeValues: "HomeController",
                 htmlAttributes: new { @class = "btn btn-primary" })

您尝试的第二个版本找不到“/HomeController/Create”,因此除非您的控制器是 HomeControllerController,否则您需要从 controllerName 中删除 "Controller"。

@Html.ActionLink(linkText: "Submit Request",
                 actionName: "Create",
                 controllerName: "Home",
                 routeValues: new { title = "hello", description = "world" },
                 htmlAttributes: new { @class = "btn btn-primary" })