1

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.

3 Answers 3

1

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>
Sign up to request clarification or add additional context in comments.

Comments

0

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>

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.