2

I have written a linq query from which I would like to obtain the values from that query and display the results in a table. I keep getting an error that reads as shown below

The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery1[<>f__AnonymousType56[System.Int32,System.String,System.String,System.String,System.String,System.Nullable1[System.DateTime]]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[App.Web.marketingdbclients_invalidEmails]'

When I scaffolded the view from the controller I picked one of my tables as the model and I don't think that is the right way to do it. What is the right type of model to pick if I want to display results from a linq query and or stored procedure because I also have a stored procedure that does the same thing as the linq query so perhaps I could use the sp as an alternative

public class InvalidEmailsController : Controller
{
    private MarketingDBEntitiesModel db = new MarketingDBEntitiesModel();

    public ActionResult InvalidEmails(int UploadId)
    {
        //var emails = db.sp_marketing_getInvalidEmailsByUploadId(UploadId);

        var emailTable = (from mie in db.marketingdbclients_invalidEmails
                          join mdt in db.marketingdbclients_dataTable on mie.ClientId equals mdt.ClientId
                          where mdt.UploadId == 88 
                          select new  
                                 {
                                      mie.ClientId,  mie.Email1, mie.Email2, 
                                      mie.Email3, mie.Email4, mie.DateStamp
                                 });

        return View(emailTable);              
    }
}

This is my view which is bound to a a table as model (which I think is the wrong method)

@model IEnumerable<App.Web.marketingdbclients_invalidEmails>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.ClientId)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Email1)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Email2)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Email3)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Email4)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.DateStamp)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.ClientId)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Email1)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Email2)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Email3)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Email4)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.DateStamp)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.invalidEmailId }) |
            @Html.ActionLink("Details", "Details", new { id=item.invalidEmailId }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.invalidEmailId })
        </td>
    </tr>
}

</table>
2
  • "select mie" instead of "select new {mie.ClientId, mie.Email1, mie.Email2, mie.Email3, mie.Email4, mie.DateStamp}" will work Commented Sep 27, 2019 at 6:53
  • That's because "new {...}" returns AnonymousType, not App.Web.marketingdbclients_invalidEmails. Also you can't (at least with EF 6) write something like "select new App.Web.marketingdbclients_invalidEmails { ClientId = mie.ClientId... }", you should create DTO class for this purpose. Commented Sep 27, 2019 at 6:56

2 Answers 2

1

You are trying to pass an anonymous type to 'App.Web.marketingdbclients_invalidEmails' so that's the way it produces an error.

You need to create a new View Model with required properties that you want to project from LINQ and show in your view.

public class marketingdbclients_invalidEmailsVM
{
    public int ClientId { get; set; }
    public string Email1 { get; set; }
    public string Email2 { get; set; }
    public string Email3 { get; set; }
    public string Email4 { get; set; }
    public DateTime DateStamp { get; set; }
}

Now use this view model for your LINQ

public ActionResult InvalidEmails(int UploadId)
{
        //var emails = db.sp_marketing_getInvalidEmailsByUploadId(UploadId);

        var emailTable = (from mie in db.marketingdbclients_invalidEmails
                          join mdt in db.marketingdbclients_dataTable on mie.ClientId equals mdt.ClientId
                          where mdt.UploadId == 88
                          select new marketingdbclients_invalidEmailsVM
                          {
                              ClientId = mie.ClientId,
                              Email1=mie.Email1,
                              Email2=mie.Email2,
                              Email3=mie.Email3,
                              Email4=mie.Email4,
                              DateStamp=mie.DateStamp
                          });

        return View(emailTable);
    }

And bind your model using a new created View Model.

@model IEnumerable<App.Web.marketingdbclients_invalidEmailsVM>    

Hopefully, it will resolve your problem.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks it works like a charm. Could I use the same method if I want to use a stored procedure? I am asking because I read somewhere that stored procedures are faster
Yes, But store procedures are a little bit complicated. I think you don't need to implement store procedures for these little tasks untill you have a real problem.
0

If you have a model in view:

@model IEnumerable<App.Web.marketingdbclients_invalidEmails>

You must return the same type from the controller action. Change this:

... select new  {...});

to this:

... select new App.Web.marketingdbclients_invalidEmails {...}).ToList();

and model to:

@model List<App.Web.marketingdbclients_invalidEmails>

Good luck

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.