I have to pass an array to my _layout page (which does not have an @model). The array must come from an controller method such as:
public IActionResult AssignLoan()
{
EntityDetails entity = new EntityDetails();
entity.Name = (from o in _context.EntityDetails
select o.Name).ToString();
entity.Name.ToArray();
ViewData["populatedropdown"] = entity;
return View();
}
and the array must be passed to this section of the _layout view and must be set equal to var countries.
var countries = ["Slovakia", "Slovenia", "Solomon Islands", "Somalia", "South Africa"];
Use a view component. Add a folder called ViewComponents
to your project and add the following class. Obviously, change Foo
to a name that actually describes what this does.
public class FooViewComponent : ViewComponent
{
public async Task<IViewComponentResult> InvokeAsync()
{
var list = // get your list
return View(list);
}
}
Then, add a view at Views\\Shared\\Components\\Foo\\Default.cshtml
. Inside, put the HTML that renders you drop down list.
Finally, in your layout:
@await Component.InvokeAsync("Foo")
Where "Foo"
is the name of your view component minus the ViewComponent
part. View components are injectable, so if you need access to your context or something, you simply add a constructor that receives that as a param and assign it to an ivar, just as you would with a controller. Also, InvokeAsync
can be passed parameters if you need them. For example:
public async Task<IViewComponentResult> InvokeAsync(string foo)
And then:
@await Component.InvokeAsync("Foo", new { foo = "bar" })
More information can be found in the docs .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.