简体   繁体   中英

How to bind anonymous type to viewModel in ASP.NET-MVC

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.

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>

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>
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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM