Scuba Steve Scuba Steve - 1 month ago 11
ASP.NET (C#) Question

asp.net passing an id between views in different controllers

So I have two models with a one to many relationship. Callout, and shift offer. Callout can have many shift offers.

I want to pass my callout_id to the shift-offer controller. I've done this by modifying the Index() method as follows:

public async Task<ActionResult> Index(long? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
Debug.WriteLine("Callout ID cannot be null");
}
var shift_Offers = db.Shift_Offers.Where(s => s.callout_id_fk == id);
return View(await shift_Offers.ToListAsync());
}


I've also modified one of my HTML action links to call a different page:

@Html.ActionLink("View Callout",
"Index",
"ShiftOffer",
new { area = ""},
new { id=item.callout_id_pk }) |


But here's the kicker, when I try to call Index(long id), it's throwing that bad request error. Why isn't my ID being passed like it is with the default scaffolded links?

Answer

Try below

@Html.ActionLink("View Callout", 
                "Index", 
                "ShiftOffer", 
                new { id=item.callout_id_pk }, 
                new { area = ""})