简体   繁体   中英

How to send item through ActionLink to ASP.NET MVC controller?

Here I have some rows in a table and am listing out the flags for my products which in this case is called item. When I want to edit the flags as on or off (yes/no), I need to get access to the whole item itself as I have no other way of identifying which item's flag I clicked on.

@foreach (var item in Model)
{
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            <ul>
                @foreach (var flag in item.Flags)
                {
                    <li>
                        @Html.DisplayFor(modelItem => flag.Name) |
                        @(flag.Enabled ? "Yes" : "No") |
                        @Html.ActionLink("Edit", "FlagEdit", new { flagId = flag.Id, itemId = item.Id })
                    </li>
                }

            </ul>
        </td>

If this isn't clear, please ask and I'll elaborate. I'm trying to send the itemId but because I am within the flags foreach, I cannot get hold of it.

when I get to the first if that checks params are not null, the check fails.

    [ActionName("FlagEdit")]
    public async Task<ActionResult> EditFlagAsync(string flagId, string itemId)
    {
        if (flagId == null || itemId == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }

        Item item = await DocDBRepo<Item>.GetItem(itemId);
        if (item == null)
        {
            return HttpNotFound();
        }

        //do update

        return View(item);
    }

I've tried searching but maybe I'm just not searching the right keywords. Can anyone please help?

尝试这个:

@Html.ActionLink("Edit", "FlagEdit", new { flagId = flag.Id, itemId = item.Id }, null)

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