0

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"];
Deepak Sharma
  • 4,124
  • 1
  • 14
  • 31
  • `.ToString()` on your query makes no sense (do you mean `.ToList()`?) And `entity.Name.ToArray();` makes no sense since you do not assign the result to anything. –  Aug 20 '18 at 08:20
  • And what is the purpose of this? What are you wanting to do with the array? –  Aug 20 '18 at 08:21
  • I am quite new to this, so the query result must be set to an array and that array must be passed to my view with no @model – stefan van schoor Aug 20 '18 at 08:22
  • I want to pass the array to my _layout view to that is populates a searchable dropdownlist – stefan van schoor Aug 20 '18 at 08:23
  • 1
    Then you are going about it all wrong. In the layout, you use `@Url.Action(...)` to call a `[ChildActionOnly]` controller method that returns a partial view (and that partial view will be strongly bound to a model, and use `@Html.DropDownListFor(m => m.SomeProperty, Model.SomeSelectList)` –  Aug 20 '18 at 08:25
  • check the answer https://stackoverflow.com/questions/13225315/pass-data-to-layout-that-are-common-to-all-pages/32725208#answer-31034711 – Deepak Sharma Aug 20 '18 at 08:30
  • I understand thank you @StephenMuecke – stefan van schoor Aug 20 '18 at 08:45
  • @StephenMuecke This is ASP.NET Core. There's no such thing as child actions. The closest thing is a view component. – Chris Pratt Aug 20 '18 at 13:28

1 Answers1

2

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.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444