C# 参数字典包含参数的空条目
我有两个视图页面使用相同的控制器和模型来更改页面布局,但我无法显示第二个页面。这是我的错误消息:C# 参数字典包含参数的空条目,c#,asp.net-mvc,asp.net-mvc-routing,C#,Asp.net Mvc,Asp.net Mvc Routing,我有两个视图页面使用相同的控制器和模型来更改页面布局,但我无法显示第二个页面。这是我的错误消息:参数字典包含UserController中system.web.mvc.actionaresult ListView(int32)方法的非空类型参数id的空条目 不确定是什么导致了这个问题,我在第一个视图页面上使用了相同的代码(工作),只是更改了视图布局 第一视图 <div> <a href="/Roster/ListView">Click Here for List view
参数字典包含UserController中system.web.mvc.actionaresult ListView(int32)方法的非空类型参数id的空条目
不确定是什么导致了这个问题,我在第一个视图页面上使用了相同的代码(工作),只是更改了视图布局
第一视图
<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>
Api控制器
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();
}
}
private UserService _userService = new UserService();
public IEnumerable<User> Get(int? id)
{
if (id.HasValue)
{
return _userService.GetUsers(id.Value);
}
throw new HttpResponseException(HttpStatusCode.NotFound);
}
private UserService\u UserService=new UserService();
公共IEnumerable Get(int?id)
{
if(id.HasValue)
{
return\u userService.GetUsers(id.Value);
}
抛出新的HttpResponseException(HttpStatusCode.NotFound);
}
您可以编写以下链接href=“/花名册/ListView”
,但操作ListView需要参数id,此处缺少该id。URL/花名册/ListView
缺少id值。您需要:
将URL更改为类似于/floster/ListView/123
通过将类型从int
更改为int?
来更改操作方法以允许使用可为空的整数。然后,在action方法中,您需要检查id
参数是否为null,并进行适当的处理,例如返回错误或忽略id
但是+id.ToString()
不应该添加参数吗?@Amina错误来自对MVC控制器的调用,而不是Web API控制器。甚至不能调用MVC控制器操作。因此,错误不是来自控制器内部,而是在控制器操作方法运行之前出现的。好的,我现在明白了。我更改了它,知道它抛出了一个错误,说没有包含我所拥有的ViewBag
的“userapirl”定义。即使它在控制器中,我还是在href=“/floster/ListView/9”
中硬编码,但仍然没有显示尝试使用@Html.ActionLink mvc helper生成正确的linkNice job Garath,使用操作链接显示应该存在的内容。这帮我解决了一个问题。谢谢