3

I am having two view pages using the same controller and model as a way to change the page layout but i am having trouble displaying my second page. This is my error message: The parameters dictionary contains a null entry for parameter id of non-nullable type for method system.web.mvc.actionaresult ListView(int32) in UserController

Not sure what is causing the problem i used the same code for the first view page(working) except just changing the view layout.

First View

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

List View

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

Controller

 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();
    }
}

Api controller

private UserService _userService = new UserService();

    public IEnumerable<User> Get(int? id)
    {
        if (id.HasValue)
        {
            return _userService.GetUsers(id.Value);
        }
        throw new HttpResponseException(HttpStatusCode.NotFound);
    }
tereško
  • 58,060
  • 25
  • 98
  • 150
Masriyah
  • 2,445
  • 11
  • 49
  • 91

2 Answers2

2

You write following link href="/Roster/ListView" but action ListView require parameter id, which is missing here.

KesaVan
  • 1,031
  • 16
  • 32
Piotr Stapp
  • 19,392
  • 11
  • 68
  • 116
2

The URL /Roster/ListView is missing an ID value. You'll need to either:

  1. Change the URL to something like /Roster/ListView/123
  2. Change the action method to allow a nullable integer by changing the type from int to int?. Then in the action method you need to check if the id parameter is null or not and deal with that appropriately, such as by returning an error, or just ignoring the id.
Eilon
  • 25,582
  • 3
  • 84
  • 102
  • but shouldn't the `+ id.ToString()` should add the parameter? – Masriyah Apr 09 '13 at 04:13
  • @Amina the error is coming from the call to the MVC controller, not the Web API controller. The MVC controller action cannot even be called. So the error isn't coming from *within* the controller, it's coming before the controller action method is even run. – Eilon Apr 09 '13 at 04:24
  • ok i see that now. I changed it and know it is throwing an error saying that `doesn't contain a definition for "UserApiURL"` the `ViewBag` i have. even though it is in the controller – Masriyah Apr 09 '13 at 05:05