0

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?

2
  • what is exactly your problem? itemId is not reaching the controlling method? Your ActionLink looks fine Commented Dec 30, 2017 at 7:43
  • Have updated with controller and exact point where it fails. flagId is not null so I know the actionLink is ok, I'm just not able to send my item from outer foreach Commented Dec 30, 2017 at 7:47

1 Answer 1

1

Try this:

@Html.ActionLink("Edit", "FlagEdit", new { flagId = flag.Id, itemId = item.Id }, null)
Sign up to request clarification or add additional context in comments.

1 Comment

ActionLink is an extension method with +9 overloads, if you don't set null after the routeValues, the framework will think that these new { flagId = flag.Id, itemId = item.Id } are the htmlAttributes param. So your params on the controller level will stay null in this case. But when you pass the fourth null parameter, then the param match is correct

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.