Parameters字典包含一个空的parameter条目

本文关键字:parameter 条目 字典 包含一 Parameters | 更新日期: 2023-09-27 18:08:04

我有两个视图页面使用相同的控制器和模型作为改变页面布局的一种方式,但我有麻烦显示我的第二页。这是我的错误信息:The parameters dictionary contains a null entry for parameter id of non-nullable type for method system.web.mvc.actionaresult ListView(int32) in UserController

不确定是什么原因导致的问题,我使用相同的代码为第一个视图页面(工作),除了只是改变视图布局。

第一个观点

<div>
<a href="/Roster/ListView">Click Here for List view</a>
</div>
<section id="users" data-bind="foreach: Users">
    <div id="nameImage">
        <figure id="content">
            <img width="158" height="158" alt="Gravatar" data-bind="attr:{src: GravatarUrl}"/>
            <figcaption>
                <a title="Email" id="emailIcon" class="icon-envelope icon-white" data-bind="attr:{'href':'mailto:' + Email()}"></a>
                <a title="Profile" id="profileIcon" class="icon-user icon-white"></a>
            </figcaption>
        </figure>
        <p data-bind="text:Name"></p>
        </div>
    </section>
</section>
列表视图

<div class="accordion-inner">
<div data-bind="foreach: Users">
    <div>
        <img width="158" height="158" alt="Gravatar" data-bind="attr:{src: GravatarUrl}"/>
        <p data-bind="text:Name"></p>
    </div>
</div>

控制器

 public ActionResult View(int id)
    {
        // get the menu from the cache, by Id
        ViewBag.SideBarMenu = SideMenuManager.GetRootMenu(id);
        ViewBag.UserApiURL = "/api/User/" + id.ToString();
        return View(); 
    }
    public ActionResult ListView(int id)
    {
        // get the menu from the cache, by Id
        ViewBag.SideBarMenu = SideMenuManager.GetRootMenu(id);
        ViewBag.UserApiURL = "/api/User/" + id.ToString();
        return View();
    }
}
<<p> Api控制器/strong>
private UserService _userService = new UserService();
    public IEnumerable<User> Get(int? id)
    {
        if (id.HasValue)
        {
            return _userService.GetUsers(id.Value);
        }
        throw new HttpResponseException(HttpStatusCode.NotFound);
    }

Parameters字典包含一个空的parameter条目

URL /Roster/ListView缺少ID值。您需要:

  1. 将URL更改为/Roster/ListView/123
  2. 通过将类型从int更改为int?,更改动作方法以允许可空整数。然后在动作方法中,您需要检查id参数是否为空,并适当地处理它,例如通过返回错误,或者只是忽略id。

您写以下链接href="/Roster/ListView",但操作ListView需要parameter id,这是这里缺失的