asp.net MVC参数始终获取默认值

时间:2013-03-18 15:37:45

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

考虑这个控制器动作:

public ActionResult List(int userid = 0)
{
    var campaigns = db.Dyn_Campaigns.Where(c => c.UserID == userid);
    return View(campaigns.ToList());
}

模型中的UserID字段是int。

这是观点:

@model IEnumerable<KnockoutExample.Models.Dyn_Campaigns>

@{
    ViewBag.Title = "List";
 }

<h2>List</h2>

<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
    <th>
        @Html.DisplayNameFor(model => model.DateCreation)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.CompanyID)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.UserID)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.Name)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.Descr)
    </th>
    ...
</tr>

@foreach (var item in Model) {
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.DateCreation)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.CompanyID)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.UserID)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Name)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Descr)
    </td>
    ...
    <td>
        @Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
        @Html.ActionLink("Details", "Details", new { id=item.ID }) |
        @Html.ActionLink("Delete", "Delete", new { id=item.ID })
    </td>
</tr>
}

</table>

这是我在global.asax中的自定义路由:

  config.Routes.MapHttpRoute("MyRoute", "{controller}/{action}/{userid}",
  new { controller = "Campaigns", action = "List", userid = UrlParameter.Optional });

问题是userid参数始终获得默认值(0)。 我在这里做错了什么?

4 个答案:

答案 0 :(得分:1)

因为UrlParameter.Optional不是int。 userid必须是int,否则找不到userid。 例如: 在你的控制者中:

public ActionResult List(int userid = 0)
{
...
}

在你的模型中(例如)

Myclass{
    int id;
}

在您看来:

 @model MyApp.Model.MyClass
 ...
    new { controller = "Campaigns", action = "List", userid =  model.id});

在Asp.net MVC 4中,视图中的参数和你的控制器必须是相同的类型和名称。

答案 1 :(得分:1)

按如下方式更改您的registerRoutes

public static void RegisterRoutes(RouteCollection routes)
            {
                routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
     routes.MapRoute("MyRoute", "{controller}/{action}/{userid}",
                    new { controller = "Campaigns", action = "List", userid = UrlParameter.Optional });    

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


            }

答案 2 :(得分:0)

改变参数:

public ActionResult List(int userid = 0)

要:

public ActionResult List(int id)

解决了这个问题。但我仍然不明白为什么该方法不接受相同类型但具有不同名称的参数。

RouteConfig看起来像这样:

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("MyRoute", "{controller}/{action}/{userid}",
            new { controller = "Campaigns", action = "List", userid = UrlParameter.Optional });
    }

答案 3 :(得分:-1)

因为如果缺少idid,那么它将作为参数获得0,它应该是可以为空的

 public ActionResult List(int? userid)
{
var campaigns = db.Dyn_Campaigns.Where(c => c.UserID == userid);
return View(campaigns.ToList());
}

根据您的修改

进行修改

您正在传递参数名称id,它应该是userid

@Html.ActionLink("Edit", "Edit", new { userid=item.ID }) |
    @Html.ActionLink("Details", "Details", new { userid=item.ID }) |
    @Html.ActionLink("Delete", "Delete", new { userid=item.ID })