为什么我的参数是空的?

时间:2016-01-28 08:46:29

标签: c# asp.net-mvc-4

我尝试在url中传递一个param,但我的param总是为null:

查看:

@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 = "" });

    }

你能告诉我什么是错的吗?

2 个答案:

答案 0 :(得分:0)

@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 
    }

答案 1 :(得分:0)

这是BeginForm应该具有的方法:

Html.BeginForm(actionName, controllerName, routeValues)

在查询字符串(或url)中传递任何值的更好方法是在routeValues部分中添加数据。 actionNamecontrollerName仅应用于特定操作和控制器发送请求的位置。所以以这种方式改变你的形式:

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

通过这种方式,您不应该遇到路由问题。