0

Partial view is called in layout.cshtml, I am unaware of how to pass parameter

   @Html.RenderPartialAsync("_DatePickerPartial","passString parameter");

Partial view

<div class="Date-picker>

 <label for=" ">@string parameter here</label>

</div>

I want to change label when partial view is called in different pages. Also, if I could pass multiple parameters through partial view in layout. Open to use different method like using html tag helper/tag helpers

Qing Guo
  • 6,041
  • 1
  • 2
  • 10
sumitkhatrii
  • 116
  • 2
  • 5
  • 20
  • Does this answer your question? [How to pass parameters to a partial view in ASP.NET MVC?](https://stackoverflow.com/questions/6549541/how-to-pass-parameters-to-a-partial-view-in-asp-net-mvc) – pfx Dec 05 '22 at 06:02
  • I dont have controller for the partial view as in the link mentioned. – sumitkhatrii Dec 05 '22 at 06:04
  • @pfx using data dictionary it showing that ViewDataDictionary doesnot contain a constructor that takes 0 arguments – sumitkhatrii Dec 05 '22 at 06:12

1 Answers1

2

Below is a work demo, you can refer to it:

_layout:

           @{
                ViewData["passing string"] = "bbb";
            }
          
            <partial name="_DatePickerPartial"  view-data="ViewData" />

_DatePickerPartial:

<div class="Date-picker>

    <label for=" ">@ViewData["passing string"]</label>

</div>

OR:

@await Html.PartialAsync("_DatePickerPartial",
            
            new ViewDataDictionary(ViewData)
            {
            { "passString parameter", "sss" }
            })

_DatePickerPartial:

<div class="Date-picker>

    <label for=" ">@ViewData["passString parameter"]</label>

</div>

The third way:

@await Html.PartialAsync("_DatePickerPartial",

            new ViewDataDictionary(ViewData)
            {
            { "Property1", "Value1" } , { "Property2", "Value2" }})

_DatePickerPartial:

<div class="Date-picker>

    <label for=" ">@ViewData["Property1"]</label>
    <label for=" ">@ViewData["Property2"]</label>

</div>
Qing Guo
  • 6,041
  • 1
  • 2
  • 10