itzick binder
itzick binder

Reputation: 572

MVC View doesn't get ID of model

I'm new in MVC and trying my first project.

I have a database with several tables and I want to create controller for each table.

I started with Disc that has ID and DiscNum, both are of type int.

This is the controller I created:

public class DiscController : Controller
{
    // GET: Disc
    public ActionResult Index()
    {
        var entities = new MovieDBEntities();

        return View(entities.Disc);
    }

    // GET: Disc/Details/5
    public ActionResult Details(int id)
    {
        return View();
    }

    // GET: Disc/Create
    public ActionResult Create()
    {
        return View();
    }

    // POST: Disc/Create
    [HttpPost]
    public ActionResult Create(FormCollection collection)
    {
        try
        {
            // TODO: Add insert logic here

            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }

    // GET: Disc/Edit/5
    public ActionResult Edit(int id)
    {
        return View();
    }

    // POST: Disc/Edit/5
    [HttpPost]
    public ActionResult Edit(int id, FormCollection collection)
    {
        try
        {
            // TODO: Add update logic here

            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }

    // GET: Disc/Delete/5
    public ActionResult Delete(int id)
    {
        return View();
    }

    // POST: Disc/Delete/5
    [HttpPost]
    public ActionResult Delete(int id, FormCollection collection)
    {
        try
        {
            // TODO: Add delete logic here

            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }
}

And this is my Index.cshtml file:

@model IEnumerable<Movies.Models.Disc>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.DiscNum)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.DiscNum)
        </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>

Everything is going fine but the details View, all the other views are OK.

This is my Details view:

@model Movies.Models.Disc

@{
    ViewBag.Title = "Details";
}

<h2>Details</h2>

<div>
    <h4>Disc</h4>
    <hr />
    <dl class="dl-horizontal">
        <dt>
            @Html.DisplayNameFor(model => model.DiscNum)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.DiscNum)
        </dd>

    </dl>
</div>
<p>
    @Html.ActionLink("Edit", "Edit", new { id = Model.ID }) |
    @Html.ActionLink("Back to List", "Index")
</p>

When I try to open it I get an error that says: System.NullReferenceException. The error is in the row that says: @Html.ActionLink("Edit", "Edit", new { id = Model.ID }) |

Model is probably null.

How do i fix it?

Can someone give me a hand?

Thank you in advance!

Upvotes: 0

Views: 3239

Answers (1)

Thejaswy
Thejaswy

Reputation: 301

In the get details view you need to pass the model to the view

[HttpGet]
public ActionResult Details(int id)
{
    var model = GetDetailsById(id);
    return View(model);
}

Upvotes: 1

Related Questions