MVC Return List of Columns Other Than Full Model

Go To StackoverFlow.com

1

I have a Model created with a table called "Customers".

It contains the following fields:

  • CustomerProfileID
  • CustomerName
  • BirthDate
  • Address1
  • City
  • State
  • Zip
  • CreatedOn
  • CreatedBy

I did a "Function Import" of a Stored Procedure (CustomerProfiles_Search) that Searches the table. (I cannot post the Sproc due to sensitive information). The "Returns a Collection Of" is set to "Entities: CustomerProfile". It returns the following:

  • CustomerName
  • BirthDate
  • Address1

Now here is where I get confused. I have my Controller that is passing in the fields to search by and then passes it into my "Gateway" which returns a list.

My Controller:

public ActionResult GetRowCount(string CustomerName, string BirthDate, string Address1, string City, string State, string Zip)
{
    List<CustomerProfile> searchResults = CustomerProfileGateway.Search(CustomerName, BirthDate, Address1, City, State, Zip);

    int count = searchResults.Count();
    string rowCount = count.ToString();

    if (Request.IsAjaxRequest())
        return Content(rowCount);
    else
        return RedirectToAction("Index", "OneView");
}

... Inside my CustomerProfileGateway.cs file:

public static List<CustomerProfile> Search(string CustomerName, string BirthDate, string Address1, string City, string State, int Zip)
{
    using (ModelEntities context = new ModelEntities())
    {
        return context.CustomerProfiles_Search(CustomerName, BirthDate, Address1, City, State, Zip).ToList();
    }
}

However, when I try to output the results to a List, I get an error when I do:

The data reader is incompatible with the specified 'SVPModel.CustomerProfile'. A member of the type, 'CustomerProfileID', does not have a corresponding column in the data reader with the same name.

I think I need to create a Model of what is returned.

I created the following:

namespace Project.ABC.Objects
{
    class SearchModel
    {
            public class SearchResults
            {
                public string CustomerName { get; set; }
                public string BirthDate { get; set; }
                public string Address1 { get; set; }
            }
    }
}

But I don't know how to do this? Can someone please show me what I am doing wrong here. Any help is greatly appreciated.

2012-04-04 17:22
by Turp


0

The problem is that your sproc is not returning all the columns you are requiring by saying that it should map to a CustomerProfile.

When it tries to read the properties from the DataReader, it can't find a column called CustomerProfileID, hence the message:

A member of the type, 'CustomerProfileID', does not have a corresponding column in the data reader with the same name

Your SearchResults looks good. You just have to set "Returns a Collection Of" to "Entities: SearchResults"

2012-04-04 17:55
by Pedro
That's what I tried to do, @Pedro. When I look at the dropdown menu of "Entities", my "SearchResults" do not show up in there. How do I make this happen? My SearchModel is in my Objects folder that also has the main model named SVPModel.edmx. Where am I going wrong - Turp 2012-04-04 17:59
Honestly, I never used the UI for that. But shouldn't your SearchResults inherit from EntityObject? Try this: comment your SearchResults and create a new entity in the edmx designer with the 3 properties you need. It should do it I guess, but again, I'm not an expert on this.. - Pedro 2012-04-04 18:08
Ads