Beslinda N.
Beslinda N.

Reputation: 5326

Quering data using linq C# MVC4

I have two tables one card and the other one registereddatatime. The first table keeps the data of the cards, and the second keep the data of the card date and time when has been scanned.

So I Have a list of all the cards and when I click in the cards i want to get the time and date when the card has been scanned.

So in the controller I have the action result like this :

public class CardController : Controller
    {
        private readonly ICardDataSource _db;
        public  CardController(ICardDataSource db)
        {
            _db = db;
        }

public ActionResult details(int id)
        {


            var model = _db.Cards.Single(c => c.CardId == id);;


            return View(model);
        }

And in the index view I use the following html helper:

<ul>

    @foreach (var card in Model)
    {
        <li>@Html.ActionLink(card.Description, "details", "Card", new { card.CardId} , null);</li>
    }

    </ul>

and in the detail view I want to show the registered data for every card. When I load it says that null reference is sent instead of int32.

Detail View :

@model Card.Manager.Models.Card

@{
    ViewBag.Title = "detail";
}

<h2>Detail</h2>

@foreach (var e in Model.RegistrationDTs)
{
    @e.RegistrationDateTime
}

Upvotes: 0

Views: 60

Answers (1)

Radu Porumb
Radu Porumb

Reputation: 785

Bad variable name in link. The parameter name you have in your controller action int id needs to be the same name you pass when you construct the link.

@Html.ActionLink(card.Description, "details", "Card", new { id = card.CardId } , null);

Upvotes: 3

Related Questions