Making my viewModel Work and it has to be a list

时间:2016-02-12 19:46:54

标签: c# asp.net-mvc

I get an error in the matchingSubsViewModels(I know if it isnt a list, the error doest appear) but it has to be a List, becasue my viewModel has to show Substitutes and Distances Like in a index

The Error is "List does not contain a difinition for 'Distance' and 'Substitute '"

My viewModel

public class MatchingSubstituteViewModel
{
    public Substitute Substitute { get; set; }
    public decimal Distance { get; set;}
}

My Controller

 public ActionResult Test(FindMatchRequestModel request)
    {
        //TestData Find subject and level  
        var list = FindSubstituteSubjectLevels(new FindMatchRequestModel()
        {
            LevelID = 1,
            SubjectID = 3
        });


        var school = db.Schools.ToList().FirstOrDefault();
        var schoolAddress = school.HomeAddress;


        List<MatchingSubstituteViewModel> matchingSubsViewModels = new List<MatchingSubstituteViewModel>();

        foreach (var item in list)
        {
            var substtitute = item.Substitute;
            var subAddress = substtitute.HomeAddress;

            var distance = HomeAddress.GetGeographicDistance(schoolAddress.Longitude, schoolAddress.Latitude, subAddress.Longitude, subAddress.Latitude);

            matchingSubsViewModels = new List<MatchingSubstituteViewModel>()
            {
                Substitute =  item.Substitute, // error List<MatchingSubstituteViewModel> does not contain a difinition for 'Substitute '
                Distance = distance // error List<MatchingSubstituteViewModel> does not contain a difinition for 'Distance'
            };
        }
        return View(matchingSubsViewModels);
    }

1 个答案:

答案 0 :(得分:0)

<pre>
 List<MatchingSubstituteViewModel> lstMatchingSubsViewModels = new List<MatchingSubstituteViewModel>();

    foreach (var item in list)
    {
        var substtitute = item.Substitute;
        var subAddress = substtitute.HomeAddress;

        var distance = HomeAddress.GetGeographicDistance(schoolAddress.Longitude, schoolAddress.Latitude, subAddress.Longitude, subAddress.Latitude);

        matchingSubsViewModels = new MatchingSubstituteViewModel()
        {
            Substitute =  item.Substitute, // errors List<MatchingSubstituteViewModel> does not contain a difinition for 'Substitute '
            Distance = distance // errors List<MatchingSubstituteViewModel> does not contain a difinition for 'distance'
        };

     lstMatchingSubsViewModels .add(matchingSubsViewModels );
    }

   return View(lstMatchingSubsViewModels );


   // and in the view
  @Model List<MatchingSubstituteViewModel>

 foreach(var cm in @Model) {
 cm.Substitute 
 }