locked
Passing id using asp-route-id RRS feed

  • Question

  • User-501297529 posted

    I want to pass the id using asp-route-id using a a hyperlink. Here is the code for the View that shows the id:

    <div>
        <span>Items's: </span>
        <ul>
            @foreach (var Item in Model.Items)
            {
                <li>
    
                    <a asp-area="Admin" asp-controller="Item" asp-action="Item" asp-route-id="@Item.ItemId">@Item.ItemId</a> 
                </li>
            }
        </ul>
    </div>

    On the View that displays the details once I click on the id is where I'm having an issue. I don't want to loop thru each item id that doesn't sound right. I just want to display the details of the id that I clicked on. Is there a way to bring the Title and AsOf without looping thru each item. I feel like some of the code below is unnecessary.

     <div class="row">
                        <div class="col-md-12">
    
                            @if ((Model.Items.Any(i => i.IsCurrent)))
                            {
                                <ul>
                                    @foreach (var Item in Model.Items)
                                    {
                                        if (Item.IsCurrent)
                                        {
                                            <li>
                                                @Item.Title as of  @Item.AsOf
                                               
                                            </li>
                                        }
                                    }
                                </ul>
                            }
                        </div>
                    </div>

    Thursday, July 25, 2019 1:54 PM

Answers

  • User475983607 posted

    I want to show the record that I click on in the first code example.

    <a asp-area="Admin" asp-controller="Item" asp-action="Item" asp-route-id="@Item.ItemId">@Item.ItemId</a>

    Where I'm passing the ItemId once I click on that id it should show only the Title and AsOf date of that id that i clicked. So it should show only 1 record. Does that make sense?

    There are few pieces.  Create an "Item" Action with an [HttpGet] as links cause the browser to submit an HTTP GET.  You also need an "id" input parameter. 

            [HttpGet]
            public IActionResult Item(int id)
            {
                return View();
            }

    Since you are using Areas, you need to make sure the "Item" controller is in the Admin area.

    [Area("Admin")]
    public class ItemController : Controller
    {

    Lastly, add logic to filter the results by Id.  Your code will look similar to the following.

            [HttpGet]
            public IActionResult Index(int id)
            {
                var model = _context.Product.Where(p => p.ProductId == id).FirstOrDefault();
                return View(model);
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 25, 2019 3:32 PM

All replies

  • User475983607 posted

    It's hard to understand the intent.... If you only what IsCurrent records in the View, then filter the records before passing the model to the View.

    Thursday, July 25, 2019 1:58 PM
  • User-501297529 posted

    mgebhard

    It's hard to understand the intent.... If you only what IsCurrent records in the View, then filter the records before passing the model to the View.

    I want to show the record that I click on in the first code example.

    <a asp-area="Admin" asp-controller="Item" asp-action="Item" asp-route-id="@Item.ItemId">@Item.ItemId</a>

    Where I'm passing the ItemId once I click on that id it should show only the Title and AsOf date of that id that i clicked. So it should show only 1 record. Does that make sense?

    Thursday, July 25, 2019 2:04 PM
  • User475983607 posted

    I want to show the record that I click on in the first code example.

    <a asp-area="Admin" asp-controller="Item" asp-action="Item" asp-route-id="@Item.ItemId">@Item.ItemId</a>

    Where I'm passing the ItemId once I click on that id it should show only the Title and AsOf date of that id that i clicked. So it should show only 1 record. Does that make sense?

    There are few pieces.  Create an "Item" Action with an [HttpGet] as links cause the browser to submit an HTTP GET.  You also need an "id" input parameter. 

            [HttpGet]
            public IActionResult Item(int id)
            {
                return View();
            }

    Since you are using Areas, you need to make sure the "Item" controller is in the Admin area.

    [Area("Admin")]
    public class ItemController : Controller
    {

    Lastly, add logic to filter the results by Id.  Your code will look similar to the following.

            [HttpGet]
            public IActionResult Index(int id)
            {
                var model = _context.Product.Where(p => p.ProductId == id).FirstOrDefault();
                return View(model);
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 25, 2019 3:32 PM