title: How to pass Id from One View to Another MVC4
body: <p><strong>Problem:</strong> 
  I have List of Categories with SubCategories, so when i click on SubCategory item it redirect me to List of Entries. There along the list it has Create ActionLink. So the problem is passing the SubCategoryId when I click SubCategoryItem for the Create ActionLink. Without the CreateActionLink it lists the entries but with it, it gives an error in the index view:</p>
  
  <pre><code>Object reference not set to an instance of an object.
  Line 6:  
  Line 7:  &lt;p&gt;
  Line 8:      @Html.ActionLink("Create New", "Create", new { subCategoryId = @Model.SubCategoryId})
  Line 9:  &lt;/p&gt;
  Line 10: 
  </code></pre>
  
  <p>I understand that i am passing null reference and the question is how to avoid that? 
  Here is my code:</p>
  
  <p>Controller:</p>
  
  <pre><code>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();
          }
      }
  
  }
  </code></pre>
  
  <p>IndexView:</p>
  
  <pre><code>@model PasswordCloud.Domain.Models.SubCategory
  
  @{
  ViewBag.Title = "Index";
  }
  
  &lt;p&gt;
  @Html.ActionLink("Create New", "Create", new { subCategoryId = @Model.SubCategoryId })
  &lt;/p&gt;
  
  @{Html.RenderPartial("_EntryList",Model.EntryList);}
  </code></pre>
  
  <p>PartialView:</p>
  
  <pre><code>@model IEnumerable&lt;PasswordCloud.Domain.Models.Entry&gt;
  
  &lt;table&gt;
  &lt;tr&gt;
      &lt;th&gt;
          @Html.DisplayNameFor(model =&gt; model.Title)
      &lt;/th&gt;
      &lt;th&gt;
          @Html.DisplayNameFor(model =&gt; model.Username)
      &lt;/th&gt;
      &lt;th&gt;
          @Html.DisplayNameFor(model =&gt; model.Password)
      &lt;/th&gt;
      &lt;th&gt;
          @Html.DisplayNameFor(model =&gt; model.Url)
      &lt;/th&gt;
      &lt;th&gt;
          @Html.DisplayNameFor(model =&gt; model.Description)
      &lt;/th&gt;
      &lt;th&gt;
          @Html.DisplayNameFor(model =&gt; model.SubCategoryId)
      &lt;/th&gt;
      &lt;th&gt;&lt;/th&gt;
  &lt;/tr&gt;
  
  @foreach (var item in Model) {
  &lt;tr&gt;
      &lt;td&gt;
          @Html.DisplayFor(modelItem =&gt; item.Title)
      &lt;/td&gt;
      &lt;td&gt;
          @Html.DisplayFor(modelItem =&gt; item.Username)
      &lt;/td&gt;
      &lt;td&gt;
          @Html.DisplayFor(modelItem =&gt; item.Password)
      &lt;/td&gt;
      &lt;td&gt;
          @Html.DisplayFor(modelItem =&gt; item.Url)
      &lt;/td&gt;
      &lt;td&gt;
          @Html.DisplayFor(modelItem =&gt; item.Description)
      &lt;/td&gt;
      &lt;td&gt;
          @Html.DisplayFor(modelItem =&gt; item.SubCategoryId)
      &lt;/td&gt;
      &lt;td&gt;
          @Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
          @Html.ActionLink("Details", "Details", new { id=item.Id }) |
          @Html.ActionLink("Delete", "Delete", new { id=item.Id })
      &lt;/td&gt;
  &lt;/tr&gt;
  }
  
  &lt;/table&gt;
  </code></pre>
  
  <p>SubCategoryListItem View:</p>
  
  <pre><code>@model IEnumerable&lt;PasswordCloud.Domain.Models.SubCategory&gt;
  
  @foreach (var item in Model) {
  
  @Html.ActionLink(item.Name,"Index","Entry", new { subCategoryId = item.SubCategoryId }, null)
  }
  </code></pre>