views:

88

answers:

1

Hi,

I am facing problem in displaying the column field value at the view.

I am returning view like follows.

var TicketList = (from T in db.Tickets
                  where  T.Title.Contains("a")
                  select new {
                        customerName = (
                             from cname in db.Customers
                             where cname.CustomerId == T.CustomersId
                             select cname.FirstName + cname.LastName
                        ) ,
                        Title = T.Title
                  }).ToList();

 return View("SearchTicketsResult",TicketList);

and at the view page I have

<%foreach (TicketList ti in (IEnumerable)ViewData.Model) {%>
    <%=ti.Title%><br />
    <%=ti.customerName %>
<% } %>

I am not able to list customername or the title in the page. It says "The type or namespace name 'TicketList' could not be found (are you missing a using directive or an assembly reference?)"

Can someone guide me please.

Thanks in advance.

+2  A: 

"TicketList" is the variable name for a list of an anonymous type. There is no "TicketList" type. Casting from anonymous types can be done ("by example"), but is very brittle, and I simply don't recommend you use an anonymous type for this.

One option would be to declare my own type (in the UI's "model(s)" folder) - for example TicketSummary / TicketLite (etc), with a Title and CustomerName. Then use this type:

public class TicketSummary
{

     public string CustomerName { get; set; }
     public string Title { get; set; }
}

var ticketList = (from t in db.Tickets
                  where  t.Title.Contains("a")
                  select new TicketSummary
                  {
                      CustomerName = (
                          from cname in db.Customers
                          where cname.CustomerId == t.CustomersId
                          select cname.FirstName.ToString()
                      ).FirstOrDefault(),
                      Title = t.Title
                  }).ToList();
return ticketList;

Then you should be able to use:

<%foreach (TicketSummary ts in (IEnumerable<TicketSummary>)ViewData.Model) {%>
    <%=ts.Title%><br />
    <%=ts.customerName %>
<% } %>

In reality, I'd probably offload this work to a repository, and have a repository method that returns a list of the summary type.

Marc Gravell
I have created TicketSummary.cs in Models folder with following properties.public class TicketSummary{ public int TicketId { get; set; } public int CustomerId { get; set; } public string CustomerName { get; set; }........}I think I am not doing it right ?
ANIL MANE
Did you also change the query "new TicketSummary {...}" and the view ("foreach (TicketSummary ...")
Marc Gravell
Thanks for your comment, but I am still not able to get it.I created ITicketsRepositiory interfece IEnumerable<Ticket> ListTickets();then TicketSummary as Repository file with TicketSummary : ITicketsRepositioryand in that file I have
ANIL MANE
public IEnumerable ListTickets() { var TicketList = (from T in db.Tickets select new { customerName = (from cname in db.Customers where cname.CustomerId == T.CustomersId select cname.FirstName.ToString()), Title = T.Title }).ToList(); return TicketList;
ANIL MANE
ERROR:TicketSummary' does not contain a definition for 'Title' and no extension method 'Title' accepting a first argument of type 'CRM.Models.TicketSummary' could be found (are you missing a using directive or an assembly
ANIL MANE
TicketSummary only needs to have the properties you are projecting; Title and CustomerName... class TicketSummary { public string Title {get;set;} public string CustomerName {get;set;} }
Marc Gravell
No, don't "select new {" - you ***need*** to use your type: "select new TicketSummary {"
Marc Gravell
Thank you for quick reply, but unable to arrange where and how select new {" - you ***need*** to use your type: "select new TicketSummary I have uploaded files at http://ade-technologies.com/crm.rarI hope you can quickly modify it.Thanks in advance.
ANIL MANE
Sorry for delay... looking now...
Marc Gravell
I've updated - does that help at all? I can't really test further since there is no csproj etc in the rar
Marc Gravell
where I can find the updates. Anyway I have uploaded all the files including database sql at the place. you can check it now.
ANIL MANE
The update is in the post above.
Marc Gravell
Hey :) It worked.....Thanks a million. I really really thankful to you.Thank you so much for your time and efforts. Do let me know if I can do anything for you. You can MSN with me at [email protected]
ANIL MANE