Home > Software engineering >  How can I pass the same id value between multiple views?
How can I pass the same id value between multiple views?

Time:10-21

I have Product List Page with a List of Products without main Navigations

enter image description here

On View link click from Product List Page above, I'm able to navigate to Overview page passing the selected product id correctly but when I click on Suppliers link from main navigation I would like to pass the same id value as overview page to Suppliers page because once I click view link everything is about the selected product. What is the best way to handle this? I tried to avoid using TempData or Session.

_Navigation.cshtml look like as shown below

<nav id="navigation" class="main-navigation">
    <ul id="nav_list" class="top-level-nav">
        <li class="nav-item">
            <a href="/" class="first-level-link">Overview</a>
        </li>
        <li class="nav-item">
            <a asp-action="Index" asp-controller="Supplier" asp-area="Supplier">Suppliers</a>
        </li>        
    </ul>
</nav>

Overview Action

    [HttpGet]       
    public async Task<IActionResult> Overview(int id)
    {
        var productInfo = await _productService.GetProductDetailsByID(id);
        var productDTO = _mapper.Map<ProductOverview>(productInfo);         

        return View(productDTO);
    }

Product Overview View Model

 public class ProductOverview
 {
   public int ID { get; set; }
   public string ProductCode { get; set; }
   public string Type{ get; set; }
   public string ProductName { get; set; }
 }

Supplier Action

[HttpGet]
public async Task<IActionResult> Index(int id)
{
var suppliers= await _suppliersService.GetAllSuppliers(3); //need help pass the selected productID
var suppliersDTO = _mapper.Map<Suppliers>(suppliers);         

return View(suppliersDTO);
}

Supplier View Model

public class Supplier
{
    public int ID { get; set; }
    public int ProductID { get; set; }
    public string SupplierName { get; set; }        
 }

CodePudding user response:

If your Overview and Supplier views are treating the Navigation as the layout, or the Navigation is rendered as part of the layout, one option is to create a base or abstract model class that your outer layout (and the navigation partial view, if it's separate) consume from.

For example, you might have a class like:

public abstract class LayoutBaseClass 
{
    public abstract int ProductId { get; }
}

Then, your ProductOverview and Supplier class can derive from this base class. The Layout view can take a dependency on the abstract/base class model, making those shared properties available to the layout so you can include them in the URL being generated in the nav, while the more granular views can continue to reference the more specific classes. You don't have to change anything in your controller when returning the view, in that case -- ASP .NET Core will propagate the model into both the layout and your view.

  • Related