【问题标题】:ASP.Net MVC: Passing view model to partial viewASP.Net MVC:将视图模型传递给局部视图
【发布时间】:2013-11-13 05:22:46
【问题描述】:

我在这里阅读了很多关于 SO re: this 的帖子,但仍然不明白。

我有一个包含部分视图分页控件的视图,它需要访问与父视图相同的模型。

父视图:

model IEnumerable<Models.ExchangeBrowseViewModel>
@{
    ViewBag.Title = metaExchange.Index_PageTitle;
}
@section styles
{        
    <link rel="stylesheet" type="text/css" href="/incl/css/bp-default.css" />
    <link rel="stylesheet" type="text/css" href="/incl/css/bp-component.css" /> 
    <link rel="stylesheet" type="text/css" href="/incl/css/paging.css" />   
}
<hgroup>
    <h2>@metaExchange.Index_InlineTitle</h2>
    <h3>@ViewBag.Message</h3>
</hgroup>

<div class="container">
    <div class="row">
        <div class="col-md-2" style="border: 1px solid #000000">
             @Html.Partial("_ItemsSearch_BasicPartial");         
        </div>
        <div class="col-md-10">
            @Html.Action("Pagination", new {showWell=false, model=Model});

分页动作:

[AllowAnonymous]
    [ChildActionOnly]
    public ActionResult Pagination(ExchangeBrowseViewModel model, bool showWell)
    {
        ViewBag.ShowWell = showWell;
        return PartialView("_ItemsList_Pagination", model);
    }

分页局部视图

@model IPagedList<Models.ExchangeBrowseViewModel>
<div class="mvcpagination">
    @Html.Raw(Ajax.Pager(
        new Options
            {
                PageSize = Model.PageSize,
                TotalItemCount = Model.TotalItemCount,
                CurrentPage = Model.PageNumber,
                ItemTexts = new ItemTexts() {Next = ">>", Previous = "<<", Page = ""},
                ItemIcon = new ItemIcon() {First = "icon-backward", Previous = "icon-chevron-left", Next = "icon-chevron-right", Last = "icon-forward"},
                TooltipTitles = new TooltipTitles() {Next = "Next", Previous = "Previous", Page = "Page {0}."},
                Size = Size.normal,
                Alignment = Alignment.right,
                IsShowControls = true,
                IsShowFirstLast = true,
                CssClass = "light-theme"
            },
        new AjaxOptions
            {
                UpdateTargetId = "grid-list",
                OnBegin = "beginPaging",
                OnSuccess = "successPaging",
                OnFailure = "failurePaging"
            }, new {controller = "Requests", action = "Index", requestTitle = ViewData["requestTitle"]}))
    @if (ViewBag.ShowWell)
    {
        <div class="well">
            Showing <span class="badge badge-success">@Model.ItemStart</span> to <span class="badge badge-success">@Model.ItemEnd</span>
            of <span class="badge badge-info">@Model.TotalItemCount</span> entries</div>
    }
</div>

问题是模型在 action 方法中始终为 null,因此部分抛出 NullReference 异常试图访问模型属性。

【问题讨论】:

    标签: asp.net-mvc razor


    【解决方案1】:

    您在 Pagination 操作中传递 ExchangeBrowseViewModel 并将其绑定到控制器方法中的错误类型。

    您将整个 IEnumerable&lt;Models.ExchangeBrowseViewModel&gt; 模型从父视图传递到操作,并希望获得 ExchangeBrowseViewModel。但是模型绑定器找不到合适的数据,这就是它为空的原因。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-01
      • 2013-07-14
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      • 2012-02-02
      • 1970-01-01
      相关资源
      最近更新 更多