McKeymayker
McKeymayker

Reputation: 358

How to pass Id from One View to Another MVC4

Problem: 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:

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: 

I understand that i am passing null reference and the question is how to avoid that? Here is my code:

Controller:

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 View:

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

@foreach (var item in Model) {

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

Upvotes: 1

Views: 3425

Answers (1)

Becuzz
Becuzz

Reputation: 6866

In your Index action you never create a model and pass it to the view. So when it gets to your line where you make the action link where you use new { subCategoryId = @Model.SubCategoryId} your model is null. Thus you get a null ref exception. To fix it you need to do something like this.

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

Upvotes: 2

Related Questions