Mohamed Abuelatta
Mohamed Abuelatta

Reputation: 3

How to pass value to ASP.NET Core partial view?

I need to use the one partial view many times in the same view.

My problem is that I need to change the id of a specific element in the partial view. So, what I need is to pass a value throughout the partial view tag helper every time I call it.

For example:

The partial view

Let's call it _list

<ul id="@val">
   <li> bla bla bla </li>
   <li> bla bla bla </li>
   <li> bla bla bla </li>
   <li> bla bla bla </li>
</ul>

I need to call the partial view _list with the possibility to pass the id value.

Passing data to the partial view

<partial attripute-to-pass-value="id1" name="_list" />
<partial attripute-to-pass-value="id2" name="_list" />
<partial attripute-to-pass-value="id3" name="_list" />
<partial attripute-to-pass-value="id4" name="_list" />

I don't know which attribute can be used to do this.

Upvotes: 0

Views: 2215

Answers (1)

Li-Jyu Gao
Li-Jyu Gao

Reputation: 940

Add model tag to pass your value.

Like this:

Controller

public IActionResult Index()
{
    List<int> list = new List<int>
    {
        1,2,3,4
    };
    return View(list);
}

Index cshtml

@model List<int>

@foreach (int id in Model)
{
    <partial name="_ProductPartial" model="id" />
}

Partial cshtml

@model int

<h1>My Id = @Model</h1>

Upvotes: 5

Related Questions