为什么我的参数为空

本文关键字:参数 我的 为什么 | 更新日期: 2023-09-27 18:32:18

我尝试在url中传递参数,但我的参数总是空:

视图:

@using (Html.BeginForm("ReferenceSearch/cab", "ProductResultList"))
    {
        <button type="submit" class="btn btn-default">test</button>
    }

动作 :

public ActionResult ReferenceSearch(string reference)
    {
        ...

我在匿名的地方做了完全相同的事情,这个正在工作:

视图:

 @using (Html.BeginForm("Login/cab/la", "Authentication"))
    {
        <button type="submit" class="btn btn-default">testAuth</button>
    }

动作 :

public ActionResult Login(string fromcontroller, string fromAction, string param)
    {

我的路线 :

public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
        routes.MapRoute(
            "Auth",
            "{controller}/{action}/{fromController}/{fromAction}/{param}",
            new { controller = "Authentication", action = "Login", fromcontroller = "", fromaction = "", param = "" });
        routes.MapRoute(
            "ReferenceSearchAfterLogin",
            "{controller}/{action}/{reference}",
            new { controller = "ProductResultList", action = "ReferenceSearch", reference = "" });
    }

你能告诉我出了什么问题吗?

为什么我的参数为空

@using (Html.BeginForm("ReferenceSearch", "ProductResultList"))
    {
        <button name ="reverence" value="submit" type="submit" class="btn btn-default">test</button>
    }
The action :
public ActionResult ReferenceSearch(string reference)
    {
       //reference has now the Value submit 
    }
这是

BeginForm应该有的方法:

Html.BeginForm(actionName, controllerName, routeValues)

在查询字符串(或 url)中传递任何值的更好方法是在 routeValues 部分添加数据。 actionNamecontrollerName 应仅用于指定将请求发送到何处的操作和控制器。因此,请以这种方式更改表单:

Html.BeginForm("ReferenceSearch", "ProductResultList", new { @reference = "tab" })

这样,路由应该没有任何问题。