如何将Id从一个视图传递给另一个MVC4

问题描述:

问题: 我有SubCategories类别列表,所以当我点击SubCategory项目时,它将我重定向到条目列表。有创建ActionLink的列表。所以问题是当我为Create ActionLink单击SubCategoryItem时传递SubCategoryId。如果没有CreateActionLink它列出的条目,但有了它,它给在索引视图中的错误:如何将Id从一个视图传递给另一个MVC4

Object reference not set to an instance of an object. 
Line 6: 
Line 7: <p> 
Line 8:  @Html.ActionLink("Create New", "Create", new { subCategoryId = @Model.SubCategoryId}) 
Line 9: </p> 
Line 10: 

我明白,我路过空引用,问题是如何避免这种情况? 这里是我的代码:

控制器:

public class EntryController : Controller 
{ 
    public ActionResult Index() 
    {   
     return View(); 
    } 

    public ActionResult EntryList(int subCategoryId) 
    { 
     var entries = EntryDAL.GetEntries(subCategoryId); 
     return View("_EntryList",entries); 
    } 

    public ActionResult Create(int subCategoryId) 
    { 
     var model = new Entry(); 

     model.SubCategoryId = subCategoryId; 

     return View(model); 
    } 

    [HttpPost] 
    public ActionResult Create(Entry entry) 
    { 
     try 
     { 
      if (ModelState.IsValid) 
      { 
       var add = EntryDAL.Add(entry); 
       return RedirectToAction("Index"); 
      } 

      return View(entry); 
     } 
     catch (Exception) 
     { 
      return View(); 
     } 
    } 

} 

IndexView:

@model PasswordCloud.Domain.Models.SubCategory 

@{ 
ViewBag.Title = "Index"; 
} 

<p> 
@Html.ActionLink("Create New", "Create", new { subCategoryId = @Model.SubCategoryId }) 
</p> 

@{Html.RenderPartial("_EntryList",Model.EntryList);} 

PartialView:

@model IEnumerable<PasswordCloud.Domain.Models.Entry> 

<table> 
<tr> 
    <th> 
     @Html.DisplayNameFor(model => model.Title) 
    </th> 
    <th> 
     @Html.DisplayNameFor(model => model.Username) 
    </th> 
    <th> 
     @Html.DisplayNameFor(model => model.Password) 
    </th> 
    <th> 
     @Html.DisplayNameFor(model => model.Url) 
    </th> 
    <th> 
     @Html.DisplayNameFor(model => model.Description) 
    </th> 
    <th> 
     @Html.DisplayNameFor(model => model.SubCategoryId) 
    </th> 
    <th></th> 
</tr> 

@foreach (var item in Model) { 
<tr> 
    <td> 
     @Html.DisplayFor(modelItem => item.Title) 
    </td> 
    <td> 
     @Html.DisplayFor(modelItem => item.Username) 
    </td> 
    <td> 
     @Html.DisplayFor(modelItem => item.Password) 
    </td> 
    <td> 
     @Html.DisplayFor(modelItem => item.Url) 
    </td> 
    <td> 
     @Html.DisplayFor(modelItem => item.Description) 
    </td> 
    <td> 
     @Html.DisplayFor(modelItem => item.SubCategoryId) 
    </td> 
    <td> 
     @Html.ActionLink("Edit", "Edit", new { id=item.Id }) | 
     @Html.ActionLink("Details", "Details", new { id=item.Id }) | 
     @Html.ActionLink("Delete", "Delete", new { id=item.Id }) 
    </td> 
</tr> 
} 

</table> 

SubCategoryListItem查看:

@model IEnumerable<PasswordCloud.Domain.Models.SubCategory> 

@foreach (var item in Model) { 

@Html.ActionLink(item.Name,"Index","Entry", new { subCategoryId = item.SubCategoryId }, null) 
} 
+0

我刚刚读了两遍你的问题,完全不知道你在做什么。请重申。 – im1dermike 2014-10-27 20:00:29

+0

在Create()中,为什么你要捕获一个'Exception'但不处理它? null ref异常抛出在哪里?具体来说,什么是空? – mxmissile 2014-10-27 20:02:26

+0

它看起来像EntryList应推高SUbcategoryID,但它不是因为它不是模型的一部分。必须使用条目列表创建模型类,或者在ViewData/ViewBag中推入SubdcategoryID。 – 2014-10-27 20:10:30

在您的索引操作中,您从不创建模型并将其传递给视图。因此,当它到达您的线路,并且您使用new { subCategoryId = @Model.SubCategoryId}时,您的模型为空。因此你得到一个空的ref例外。要修复它,你需要做这样的事情。

public ActionResult Index() 
{   
    var model = ... 
    return View(model); 
} 
+0

感谢您的帮助,它正在处理您的建议,但现在当我包含@ {Html.RenderPartial(“_ EntryList”,Model.EntryList);}它会抛出一个错误传入字典的模型项类型为' PasswordCloud.Domain.Models.SubCategory',但是这个字典需要一个'System.Collections.Generic.IEnumerable'1 [PasswordCloud.Domain.Models.Entry]'类型的模型项。任何想法如何解决这个问题? – McKeymayker 2014-10-27 20:40:57

+0

它告诉你,Model.EntryList是一个子类,但你的_EntryList视图想要一个IEnumerable 。你有一个类型不匹配。如果您遇到问题,请提出一个新问题。这样更多的人会看到它,并能够帮助你。 – Becuzz 2014-10-27 21:47:10