So as the title says, I'm trying to populate my model from LINQ. I can populate it as a LIST but are there any other ways to get data into the model? So I've got my model class:
public class CustomersModel
{
public string CustomerId { get; set; }
public string CustomerName { get; set; }
public string PricingGroup { get; set; }
public string PrGrId { get; set; }
}
And I have a controller which works if I want to populate the model as a list:
public ActionResult Index(int? page)
{
var content = from c in db.PCustomers
where c.Cust_Wholesale_Customer == -1
where c.Cust_Account_Status != 2
join csi in db.PCustomer_Sales_Info on c.Cust_Key equals csi.CustSls_Cust_Key
join cpg in db.PCustomer_Pricing_Groups on csi.CustSls_CustPrcGrp_Key equals cpg.CustPrcGrp_Key
select new { id = c.Cust_ID, cname = c.Cust_Description, pg = cpg.CustPrcGrp_Description, pgid = cpg.CustPrcGrp_Key };
var model = new List<CustomersModel>();
foreach (var item in content)
{
model.Add(new CustomersModel { CustomerId = item.id });
model.Add(new CustomersModel { CustomerName = item.cname });
model.Add(new CustomersModel { PricingGroup = item.pg });
model.Add(new CustomersModel { PrGrId = item.pgid.ToString() });
}
return View(model);
}
But what if I don't want this to be a list? How do I add the data to the model without specifying it as a list?
public class MyModel { public IReadOnlyList<Model> Models {get;} public SomeAdditionalData Data {get;} }as a model. What do you want to achieve? What is the problem?