asp.net mvc 3 - how do i use this linq query to display data using foreach loop in MVC -
i not sure how achieve this. want use foreach loop in view display data in tabular format. data come linq query uses 3-4 tables , selects different fields display. not sure how return anonymous type error.
var info = s in context.lennoxsurveyresponses join customers in context.customers on s.surveycode equals customers.surveycode join lists in context.customerlists on customers.listid equals lists.listid join dealer in context.channels on lists.channelid equals dealer.channelid dealer.channelid == id select new { responseid = s.responseid, customerfirstname = customers.firstname, customerlastname = customers.lastname, customertestimonial = s.question5answer };
what model have declare can passed view?
a view works view models, start defining one:
public class customerviewmodel { public int responseid { get; set; } public string customerfirstname { get; set; } public string customerlastname { get; set; } public string customertestimonial { get; set; } }
and then:
public actionresult foo(string id) { var info = s in context.lennoxsurveyresponses join customers in context.customers on s.surveycode equals customers.surveycode join lists in context.customerlists on customers.listid equals lists.listid join dealer in context.channels on lists.channelid equals dealer.channelid dealer.channelid == id select new customerviewmodel { responseid = s.responseid, customerfirstname = customers.firstname, customerlastname = customers.lastname, customertestimonial = s.question5answer }; return view(info); }
and have view being typed to:
@model ienumerable<customerviewmodel>
now can loop use display template view model present information user:
@html.displayformodel()
and inside corresponding display template (~/views/home/displaytemplates/customerviewmodel.cshtml
):
@model customerviewmodel <div> <span>first name: @html.displayfor(x => x.customerfirstname)</span> <span>last name: @html.displayfor(x => x.customerlastname)</span> ... </div>
Comments
Post a Comment