Reputation: 83
I need to bind the anonymous type output to viewmodel to pass it on view. the need is to i have to bind the checkbox with the model value i am using join to fetch the value but dont know how to pass it to view. my join query is
var v = (from pd in ge.Costs
join od in ge.Services on pd.ServiceId equals od.ServiceId
join ct in ge.ServiceTypes on pd.ServiceTypeId equals ct.ServiceTypeId
where pd.ServiceTypeId.Equals(2)
select new
{
pd.CostId,
od.serviceName,
ct.ServiceTypeValue,
pd.ServiceCost
}).ToList();
my viewModel is
public class costViewModel
{
public int CostId { get; set; }
public string serviceName { get; set; }
public string ServiceTypeValue { get; set; }
public string ServiceCost { get; set; }
}
I need to bind the CostId , serviceName ,ServiceTypeValue ,ServiceCost to the view model to pass it in the view
the retrieve the model on view is
@foreach (var item in Model)
{
<input type="checkbox" name="@item.serviceName" id="@item.serviceName" value="@item.ServiceCost">@item.
}
please help.
Upvotes: 1
Views: 1621
Reputation: 348
Dont leave your select query anonymus, just pass your select with viewModed like
var v = (from pd in ge.Costs
join od in ge.Services on pd.ServiceId equals od.ServiceId
join ct in ge.ServiceTypes on pd.ServiceTypeId equals ct.ServiceTypeId
where pd.ServiceTypeId.Equals(2)
select new costViewModel()
{
CostId = pd.CostId,
serviceName = od.serviceName,
ServiceTypeValue = ct.ServiceTypeValue,
ServiceCost = pd.ServiceCost
}).ToList();
view(v);
and then pass v in view model
and on view page use
@model IEnumerable<project.ViewModel.costViewModel>
Upvotes: 1
Reputation: 1
var v = (from pd in ge.Costs
join od in ge.Services on pd.ServiceId equals od.ServiceId
join ct in ge.ServiceTypes on pd.ServiceTypeId equals ct.ServiceTypeId
where pd.ServiceTypeId.Equals(2)
select new costViewModel
{
CostId = pd.CostId,
serviceName = od.serviceName,
ServiceTypeValue = ct.ServiceTypeValue,
ServiceCost = pd.ServiceCost
}).ToList();
or anonymous
var v = (from pd in ge.Costs
join od in ge.Services on pd.ServiceId equals od.ServiceId
join ct in ge.ServiceTypes on pd.ServiceTypeId equals ct.ServiceTypeId
where pd.ServiceTypeId.Equals(2)
select new
{
CostId = pd.CostId,
serviceName = od.serviceName,
ServiceTypeValue = ct.ServiceTypeValue,
ServiceCost = pd.ServiceCost
}).ToList();
Upvotes: 0
Reputation: 51
try using costViewModel
to pass values into your View, so in short, do not leave your select
anonymous, create some of your desired objects and pass v
to View:
in your Controller:
var v = (from pd in ge.Costs
join od in ge.Services on pd.ServiceId equals od.ServiceId
join ct in ge.ServiceTypes on pd.ServiceTypeId equals ct.ServiceTypeId
where pd.ServiceTypeId.Equals(2)
select new costViewModel()
{
CostId = pd.CostId,
serviceName = od.serviceName,
ServiceTypeValue = ct.ServiceTypeValue,
ServiceCost = pd.ServiceCost
}).ToList();
return View(v);
and in your View:
@model List<costViewModel>
Upvotes: 0