Harreni
asked on
Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<System.Web.Mvc.SelectListItem>' to 'System.Collections.Generic.IEnumerable
Hello Experts,
I'm totally new to programming using MVC in .net 4.5 and using (DB first) approach beside scaffolding way to create controllers and views from existing models.
When I rum my application I got those 2 errrors:
1- Cannot implicitly convert type 'System.Collections.Generi c.IEnumera ble<System .Web.Mvc.S electListI tem>' to 'System.Collections.Generi c.IEnumera ble<WebApp lication2. Models.tb_ List_Agenc yCategory> '. An explicit conversion exists (are you missing a cast?)
2-'IEnumerable<tb_List_Age ncyCategor y>' does not contain a definition for 'Concat' and the best extension method overload 'Queryable.Concat<SelectLi stItem>(IQ ueryable<S electListI tem>, IEnumerable<SelectListItem >)' requires a receiver of type 'IQueryable<SelectListItem >'
Here is my Model Code:
my View code:
Please tell me how to fix this error.
Thanks a lot in advance.
Harreni
I'm totally new to programming using MVC in .net 4.5 and using (DB first) approach beside scaffolding way to create controllers and views from existing models.
When I rum my application I got those 2 errrors:
1- Cannot implicitly convert type 'System.Collections.Generi
2-'IEnumerable<tb_List_Age
Here is my Model Code:
namespace WebApplication2.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web.Mvc;
using System.Collections;
public partial class tb_Current_Agency
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public tb_Current_Agency()
{
this.tb_Current_Activation = new HashSet<tb_Current_Activation>();
this.tb_Current_Activation1 = new HashSet<tb_Current_Activation>();
this.tb_Current_GSBService = new HashSet<tb_Current_GSBService>();
this.tb_List_AgencyCategory = new HashSet<tb_List_AgencyCategory>();
}
public string AgencyID { get; set; }
public string AgencyArabicName { get; set; }
public string AgencyEnglishName { get; set; }
public string AgencyEnglishNameAbbreviation { get; set; }
public Nullable<int> AgencyCategoryID { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<tb_Current_Activation> tb_Current_Activation { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<tb_Current_Activation> tb_Current_Activation1 { get; set; }
public virtual tb_Current_Consumer tb_Current_Consumer { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<tb_Current_GSBService> tb_Current_GSBService { get; set; }
public virtual tb_Current_Provider tb_Current_Provider { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<tb_List_AgencyCategory> tb_List_AgencyCategory { get; set; }
//public int SelectedAgencyCategoryID { get; set; }
public class AgencyCategory
{
public int AgencyCategoryID { get; set; }
public string AgencyCategoryName { get; set; }
}
public readonly List<AgencyCategory> _Categories;
[Display(Name = "Favorite Category")]
public int SelectedAgencyCategoryID { get; set; }
public IEnumerable<tb_List_AgencyCategory> CategoryItems
{
get
{
var allCategories = _Categories.Select(f => new SelectListItem
{
Value = f.AgencyCategoryID.ToString(),
Text = f.AgencyCategoryName
}).ToList();
return DefaultCategoryItem.Concat(allCategories);
}
}
public IEnumerable<tb_List_AgencyCategory> DefaultCategoryItem
{
get
{
return Enumerable.Repeat(new SelectListItem
{
Value = "-1",
Text = "Select a flavor"
}, count: 1);
}
}
}
}
my View code:
@model PagedList.IPagedList<WebApplication2.Models.tb_Current_Agency>
@using PagedList.Mvc;
@using System.Linq;
@using WebApplication2.Models;
@{
ViewBag.Title = "Index";
}
<h2>tb_Current_Agency</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<Div class="active">
<p>
@using (Html.BeginForm("Index", "tb_Current_Agency", FormMethod.Get))
{
<b>Search by:</b>
@Html.RadioButton("searchBy", "AgencyID")<text> AgencyID</text>
@Html.RadioButton("searchBy", "AgencyArabicName")<text>AgencyArabicName</text>
<br />
@Html.RadioButton("searchBy", "AgencyCategoryID")<text>AgencyCategoryID</text>
<br />
@Html.TextBox("Search");
<input type="submit" value="Search" />
}
</p>
</Div>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.First().AgencyID)
</th>
<th>
@Html.DisplayNameFor(model => model.First().AgencyEnglishName)
</th>
<th>
@Html.DisplayNameFor(model => model.First().AgencyEnglishNameAbbreviation)
</th>
<th>
@Html.DisplayNameFor(model => model.First().AgencyCategoryID)
</th>
<th>
@*@Html.LabelFor(m => m.First().SelectedAgencyCategoryID)*@
@*@Html.DropDownListFor(m => m.SelectedFlavorId, Model.FlavorItems)*@
@Html.DropDownListFor(m => m.First().AgencyCategoryID, Model.First().CategoryItems)
@*@Html.ValidationMessageFor(m => m.First().SelectedAgencyCategoryID)*@
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.AgencyID)
</td>
<td>
@Html.DisplayFor(modelItem => item.AgencyArabicName)
</td>
<td>
@Html.DisplayFor(modelItem => item.AgencyEnglishName)
</td>
<td>
@Html.DisplayFor(modelItem => item.AgencyEnglishNameAbbreviation)
</td>
<td>
@Html.DisplayFor(modelItem => item.AgencyCategoryID)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.AgencyID }) |
@Html.ActionLink("Details", "Details", new { id = item.AgencyID }) |
@Html.ActionLink("Delete", "Delete", new { id = item.AgencyID })
</td>
</tr>
}
<tr>
@Html.PagedListPager(Model, page => Url.Action("Index", new { page, searchBy = Request.QueryString["searchBy"], search = Request.QueryString["search"] }), new PagedListRenderOptions() { Display = PagedListDisplayMode.IfNeeded, DisplayItemSliceAndTotal = true })
</tr>
</table>
Please tell me how to fix this error.
Thanks a lot in advance.
Harreni
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Not a problem Harreni, glad to help
ASKER