14

I am to new .NET core using 2.2 version. I am trying to pass data to partial view with following code :

 <partial name="_Emplyees" model="@Model.Employees" view-data="@new ViewDataDictionary(ViewData) { { "index", index }}"/>

but its giving syntax error. can someone guide how to pass data and use in partial view? Thanks in advance.

1

4 Answers 4

18

The issue is that you've got double quotes inside the view-data attribute. You need to use single quotes around the attribute value.

<partial name="_Emplyees" model="Employees" view-data='@new ViewDataDictionary(ViewData) { { "index", index } }'/>

Also, @Model is superfluous here, so I removed it.

Sign up to request clarification or add additional context in comments.

Comments

15

You could pass the ViewData to partial view like below in ASP.Net Core MVC:

1.Model:

public class TestModel
{
    public string Employees { get; set; }
}

2.View(Create.cshtml):

@model TestModel
@{ 
    ViewData["index"] = true;
}
<partial name="_Emplyees" model="@Model" view-data="ViewData" />

3.Partial View:

<h3>Index: @ViewData["index"]</h3>
@model TestModel

@if ((bool)ViewData["index"])
{
    @Model.Employees
}
else
{
    <input asp-for="Employees" type="number" class="form-control" />
}

4.Controller:

public IActionResult Create()
{
    var testmodel = new TestModel() { Employees = "aaa" };
    return View(testmodel);
}

5.Result:

enter image description here

Reference:

How to use view-data pass the data to partial view

https://learn.microsoft.com/en-us/aspnet/core/mvc/views/partial?view=aspnetcore-3.0#access-data-from-partial-views

Comments

1

See Partial views in ASP.NET Core and Partial Tag Helper in ASP.NET Core.

<partial name="Shared/_ProductPartial.cshtml" for="Product">

<partial name="_ProductViewDataPartial" for="Product" view-data="ViewData">

@await Html.PartialAsync("_ProductPartial", product)

Note:

When a partial view is instantiated, it receives a copy of the parent's ViewData dictionary.

Comments

1

As per the documentation, When a partial view is instantiated, it receives a copy of the parent's ViewData dictionary. Updates made to the data within the partial view aren't persisted to the parent view. ViewData changes in a partial view are lost when the partial view returns.

Checkout the documentation here

For me I have simply created a partial view, accessed it inside another view and used ViewData directly inside it.

~/Views/Shared/_PageTop.cshtml

<a asp-controller="Home" asp-action="Index">Go Back</a>
<h1>@ViewData["Title"]</h1>

~/Views/Create.cshtml

@{
    ViewData["Title"] = "Create";
}
<partial name="_PageTop" />

So without passing anything to partial view _PageTop, I can access parent's Title property directly using @ViewData["Title"].

Comments

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.