'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"
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.