如何从View继承PagedList <viewmodel> MVC 4 </viewmodel>的View中使用RenderAction传递Id

时间:2014-10-14 13:58:36

标签: asp.net asp.net-mvc entity-framework asp.net-mvc-4

我有渲染动作,以弹出模式渲染创建视图。但Model不允许传递Category的id,并调用错误:

The parameters dictionary contains a null entry for parameter 'categoryId' of non-nullable type 
'System.Int32' for method 'System.Web.Mvc.ActionResult Create(Int32)' in 
'PasswordCloudApp.Controllers.EntryController'. An optional parameter must be a reference type,
 a nullable type, or be declared as an optional parameter.

索引视图:

@model PagedList.IPagedList<PasswordCloudApp.ViewModels.EntryViewModel>

@{Html.RenderAction("Create", "Entry", new { categoryId = Model.CategoryId -->
  but don't allow to access .CategoryId });}

@Html.Partial("_Entries", Model)

行动方法指数:

 public ActionResult Index(int categoryId, int page = 1)
    {
        var category = _db.Categories.Find(categoryId);

        var model =
             db.Query<Entry>()
                .OrderBy(en=>en.Id)
                .Where(en=>en.CategoryId == categoryId)
                .Select(en => new EntryViewModel
                {
                    Id = en.Id,
                    Title = en.Title,
                    Username = en.Username,
                    Password = en.Password,
                    Url = en.Url,
                    Description = en.Description,
                }).ToPagedList(page, 10);

   return View(model);
 }

有关如何将CategoryId传递给RenderAction的任何建议吗?

2 个答案:

答案 0 :(得分:1)

看起来您没有填充模型中的CategoryId。

尝试:

public ActionResult Index(int categoryId, int page = 1)
{
   var category = _db.Categories.Find(categoryId);

   var model =
         db.Query<Entry>()
           .OrderBy(en => en.Id)
           .Where(en => en.CategoryId == categoryId)
           .Select(en => new EntryViewModel
            {
               Id = en.Id,
               CategoryId = en.CategoryId,
               Title = en.Title,
               Username = en.Username,
               Password = en.Password,
               Url = en.Url,
               Description = en.Description,
             }).ToPagedList(page, 10);

            return View(model);
}

答案 1 :(得分:0)

如果PagedList.IPagedList绝对是集合类型,则无法访问categoryId属性。你能用类型PagedList.IPagedList?

共享模型定义吗?