Cathal O 'Donnell
Cathal O 'Donnell

Reputation: 525

Missing a using directive or an assembly reference MVC

'IEnumerable' does not contain a definition for 'ClubName' and no extension method 'ClubName' accepting a first argument of type 'IEnumerable' could be found (are you missing a using directive or an assembly reference?)

This is the error I run into every time I try to run my code.

This is the code from my Home Controller

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using ultimateorganiser.Models;
using ultimateorganiser.ViewModels;
using System.Data;

namespace ultimateorganiser.Controllers
{
    public class HomeController : Controller
    {
        public UltimateDb db = new UltimateDb();
        ClubViewModels cvm = new ClubViewModels();
        MemberViewModels mvm = new MemberViewModels();

        //Home Page
        public ActionResult Index()
        {
            cvm.Clubs = db.Clubs.Include("clubmembers").ToList();
            cvm.NumberofClubs = cvm.Clubs.Count();
            cvm.MemberCount = 0;
            cvm.Clubs.ForEach(clb => cvm.MemberCount += clb.ClubMembers.Count());
            ViewBag.title = "Clubs List (" + cvm.NumberofClubs + ")";
            return View(cvm.Clubs);
        }

This is the View Model(ClubViewModels)

using ultimateorganiser.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;


namespace ultimateorganiser.ViewModels
{
    public class ClubViewModels
    {
        public int NumberofClubs { get; set; }
        public List<Club> Clubs { get; set; }
        public int MemberCount { get; set; }
    }
}

This is my View

@model IEnumerable<ultimateorganiser.Models.Club>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<div>
    <h4>Club</h4>
    <hr />
    <dl class="dl-horizontal">
        <dt>
            @Html.DisplayNameFor(model => model.ClubName)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.ClubName)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.ClubDescription)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.ClubDescription)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.ClubImage)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.ClubImage)
        </dd>

    </dl>
</div>
<p>
    @Html.ActionLink("Edit", "Edit", new { id = Model.ClubID }) |
    @Html.ActionLink("Back to List", "Index")
</p>

When I run it on my browser it tells me that it is a "Compilation Error", " 'IEnumerable' does not contain a definition for 'ClubName' and no extension method 'ClubName' accepting a first argument of type 'IEnumerable' could be found (are you missing a using directive or an assembly reference"

Upvotes: 0

Views: 5295

Answers (3)

ranobe
ranobe

Reputation: 51

"Hi, if you want use an IEnumerable then add"

@model IEnumerable<ultimateorganiser.Models.Club>

<div>
<h4>Club</h4>
<hr />
<dl class="dl-horizontal">
    @foreach (var item in Model)
    {
        <dt>
        @Html.DisplayNameFor(model => item.ClubName)
    </dt>

    <dd>
        @Html.DisplayFor(model => item.ClubName)
    </dd>

    <dt>
        @Html.DisplayNameFor(model => item.ClubDescription)
    </dt>

    <dd>
        @Html.DisplayFor(model => item.ClubDescription)
    </dd>

    <dt>
        @Html.DisplayNameFor(model => item.ClubImage)
    </dt>

    <dd>
        @Html.DisplayFor(model => item.ClubImage)
    </dd>
    }
    
</dl>

"If you want access to model use"

@model ultimateorganiser.Models.Club
<form asp-action="Create">
    <div class="form-group">
        <label asp-for="ClubName" class="control-label"></label>
        <input asp-for="ClubName" class="form-control" />
        <span asp-validation-for="ClubName" class="text-danger"></span>
    </div>
 </form>

Upvotes: 0

Lidaranis
Lidaranis

Reputation: 785

Your problem is the this. The @model in your view is an ienumerable but you are using it like a club. Ienumerable is a list so, you need to go through the list, item by item in order to access properties like club name. So either use a @foreach in the view if you have more than one club or get rid of the ienumerable in the @model if you have only one.

Upvotes: 1

Daniel Mann
Daniel Mann

Reputation: 59010

Look at your view:

@model IEnumerable<ultimateorganiser.Models.Club>

You're saying that the model should be an IEnumerable<Club>.

Then, you try to access a property on your model:

@Html.DisplayNameFor(model => model.ClubName)

Your model is an IEnumerable, not a Club. You need to iterate over the model.

Upvotes: 1

Related Questions