3

ページ レイアウトを変更する方法として、同じコントローラーとモデルを使用して 2 つのビュー ページを作成していますが、2 番目のページの表示に問題があります。これは私のエラーメッセージです: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();
    }
}

API コントローラー

private UserService _userService = new UserService();

    public IEnumerable<User> Get(int? id)
    {
        if (id.HasValue)
        {
            return _userService.GetUsers(id.Value);
        }
        throw new HttpResponseException(HttpStatusCode.NotFound);
    }
4

2 に答える 2

2

次のリンクを記述しますhref="/Roster/ListView"が、アクションListView requireparameter idがここにありません。

于 2013-04-09T04:03:43.563 に答える
2

URL/Roster/ListViewに ID 値がありません。次のいずれかを行う必要があります。

  1. URLを次のように変更します/Roster/ListView/123
  2. intタイプを からに変更して、null 許容整数を許可するようにアクション メソッドを変更しint?ます。次に、アクション メソッドで、idパラメーターが null かどうかを確認し、エラーを返すか、単に ID を無視するなど、適切に処理する必要があります。
于 2013-04-09T04:05:49.493 に答える