简体   繁体   中英

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?

Try below

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM