1

I am trying to call IEnumerable method in my _Layout.cshtml file. At the final I was adviced to "use html.action - to call server method that populates collection and returns partial view".

Currently I have created partial file _Dodatki.cshtml, that contains call of IEnumerable method (Aktualnosci.cs is model file):

@model IEnumerable<DluzynaSzkola.Models.Aktualnosci>

In my _Layout.cshtml I called method from my constructor with:

@Html.Action("_Dodatki", "AktualnosciController ", new {area="" })

And at the final I want to create method in my AktualnosciConstructor.cs file. Currenly I have method:

[ChildActionOnly]
[ActionName("_Dodatki")]
public ActionResult Dodatki()
{
    IList<Aktualnosci> lista = new IList<Aktualnosci>();
    return PartialView("_Dodatki", lista);
}

Unfortunately, when using syntax as above, it gives me message in compiler:

"cannot create an instance of the abstract class or interface 'IList'".

When replacing 'IList' with 'List', it gives me exception:

"System.Web.HttpException: The controller for path '/' was not found or does not implement IController."

I have no idea how in other way I can populate collection in the method.

edit: As per request, below AktualnosciController.cs definition, with no other methods:

namespace DluzynaSzkola.Controllers
{
    public class AktualnosciController : Controller
    {
        //here are other methods

        [ChildActionOnly]
        [ActionName("_Dodatki")]
        public ActionResult Dodatki()
        {
            IList<Aktualnosci> lista = new IList<Aktualnosci>();
            return PartialView("_Dodatki", lista);
        }
    }
}
adiga
  • 34,372
  • 9
  • 61
  • 83
  • Can you show us the whole controller ? Maybe excluding other methods, but at least the controller definition. – Mathieu VIALES Oct 13 '17 at 18:17
  • When do you get the `"System.Web.HttpException: The controller for path '/' was not found or does not implement IController."` exception ? When you click on the link ? – Mathieu VIALES Oct 13 '17 at 18:34
  • @Wndrr I am getting it when I start compilation of the application (with layout on main page). –  Oct 13 '17 at 18:37
  • This might be the reason but not sure... in your View you have `@Html.Action("_Dodatki", "_Layout")`... which means you are looking for the `_Dodatki` action in the `_Layout` controller.. you don't have a `_Layout` controller... you have a `Aktualnosci` controller.. so try changing to `@Html.Action("_Dodatki", "Aktualnosci")` – Grizzly Oct 13 '17 at 18:38

2 Answers2

0

as noticed by GTown-Coder your controller name seems wrong. Updated my answer accordingly.

I think that your problem might be the same as answered by this SO post.

try specifying the Area name and, if this controller is not in an area simply add an empty area name.

@Html.Action("_Dodatki", "AktualnosciController ", new {area="" })

Even if this does not solve your problem it is good practice because if this view is latter used within an area it will try to find the controller in the area and not in the root space.

Mathieu VIALES
  • 4,526
  • 3
  • 31
  • 48
  • update my answer as soon as you posted. I don't pay enough attention to the name people give to things ... it isn't the first time i miss an obvious mismatch like this -_- thanks for you sharp eye :-) – Mathieu VIALES Oct 13 '17 at 18:40
  • Now, just take the `_` out of the name of the controller. The OP doesn't have that either – Grizzly Oct 13 '17 at 18:41
  • Lol no problem. – Grizzly Oct 13 '17 at 18:42
  • @GTown-Coder Good point, guys, just corrected. But when using 'List' it gives me same exception **System.Web.HttpException: The controller for path '/' was not found or does not implement IController** in marked row: **@Html.Action("_Dodatki", "AktualnosciController ", new { area = "" })**. I am guessing, it supposed to be interface list for IEnumerable model. But IList does not work in the same context as above. –  Oct 13 '17 at 18:55
0

Allright, I have implemented changes to my project, that works fine.

My in _Layout.cshtml call is changed a bit. AktualnosciController supposed to be called just Aktualnosci !!!

<div class="kontenerDodatkiLayout hidden-xs hidden-sm">
                <div class="archiwum">Archiwum</div>
                @Html.Action("_Dodatki", "Aktualnosci", new { area = "" })
            </div>

My partial view _Dodatki.cshtml model call is changed a bit:

@model IEnumerable<DateTime>

<div class="wpisDodatki">
    @foreach (var item in Model)
    {
        <div> @Html.DisplayFor(modelItem => item)</div>
    }
    <p>test<br />test<br />test</p>
</div>

And method in my controller AktualnosciController.cs looks like that:

//[ChildActionOnly]
        [ActionName("_Dodatki")]
        public ActionResult Dodatki()
        {
            using (var context = new DluzynaContext())
            {
                var lista = context.Indeks.Select(it => it.Dzien).ToList();

                return PartialView("_Dodatki", lista);
            }
        }

in here lista is passed to my partial view _Dodatki, and it is populated with context property Indeks and model property Dzien.

Thanks guys for your help @Wndrr , @GTown-Coder.