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

3
  • Does this answer your question? How to pass parameters to a partial view in ASP.NET MVC? Commented Dec 5, 2022 at 6:02
  • I dont have controller for the partial view as in the link mentioned. Commented Dec 5, 2022 at 6:04
  • @pfx using data dictionary it showing that ViewDataDictionary doesnot contain a constructor that takes 0 arguments Commented Dec 5, 2022 at 6:12

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

2 Comments

@sumitkhatrii Please have a look at my answer.
It works, though I opted for html tag helper. Thank man @QingGuo. The answer is on point for my quesstion.

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.