Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm new to both MVC and LinqToSql. I'm trying to create a small application that lists contacts using both technologies.

What I have tried:

My Model:
C#
public class Contact
{
    [Key]
    public int Id { get; set; }
    [Required]
    public string Name { get; set; }
    [Range(18, 99)]
    public int? Age { get; set; }
    [EmailAddress]
    public string Email { get; set; }
    [Phone]
    public string Phone { get; set; }
    public Gender? Gender { get; set; }
    public string Address { get; set; }
}

public enum Gender { Male, Female }


My Controller:
C#
public class ContactController : Controller
{
    private static string conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
    LinqToSqlDataContext db = new LinqToSqlDataContext(conStr);

    // GET: Contacts
    public ActionResult Index()
    {
        IList<Contact> contactList = new List<Contact>();
        var listdata = (from c in db.Contacts select c).ToList();

        foreach (var item in listdata)
        {
            contactList.Add(new Contact()
            {
                Id = item.Id,
                Name = item.Name,
                Age = item.Age,
                Email = item.Email,
                Phone = item.Phone,
                Gender = item.Gender,
                Address = item.Address
            });
        }
        return View(contactList);
    }


My View:
@model IEnumerable<ContactsApp.Models.Contact>

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        ...
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        ...
    </tr>
}
</table>


When I run this I get the following error:
The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[ContactsApp.Contact]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[ContactsApp.Models.Contact]'.


I'd appreciate help in understanding exactly what I am doing wrong.
Thank you.
Posted
Updated 19-Apr-19 12:16pm
Comments
j snooze 19-Apr-19 17:54pm    
Why do you have
IList<contact> contactList = new List<contact>();

instead of
List<contact> contactList = new List<contact>();

?

1 solution

Well, since IList implements IEnumerable, you should be good there.

But, you didn't really read the error message. You apparently have two different Contact classes. once in a namespace called ContactsApp and the other in a namespace called ContactsApp.Models. The two are not interchangeable.

You need to either change the @model line in you Razor code or change which object you're adding to the list. Of course, I can't tell you which one to do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900