Skip to content
Loading
Implement foreachloop in mvc    
  •     class="col-md-6">    
  •         //fourth vacancy data and apply button    
  •         
  •     
  • if no vacancy admin side then below display image
    1. class="row">  
    2. //@ViewBag.message //here I am fetching controller viewbag message in model means **no content available**  
      
    admin side vacancy:
     
    client side vacancy(here I fetch the admin vacancy):
     
     
    ViewModel
    1. public class ViewModel    
    2. {    
    3.     public Career Career { getset; }    
    4.     public List Vacancy { getset; }    
    5.     [NotMapped]    
    6.     public SelectList vacancyselectlist { getset; }    
    7. }    
    8.     
    9. public class Career    
    10. {    
    11.     [Key]    
    12.     public int usercarrerid { getset; }    
    13.         
    14.     public string useremailid { getset; }    
    15.     
    16.     public string usercontactno { getset; }    
    17.     
    18.     public string userresume { getset; }    
    19.     
    20.     public Nullable<int> vacancyid { getset; }    
    21.     
    22.     [ForeignKey("vacancyid")]    
    23.     public Vacancy vacancy { getset; }    
    24.     
    25. }    
    26.     
    27. public class Vacancy    
    28. {    
    29.     public int vacancyid { getset; }    
    30.     public string vacancytitle { getset; }    
    31.     public string vacancyposition { getset; }    
    32.     public string vacancyexperience { getset; }    
    33.     public string vacancyjobdescription { getset; }    
    34.     public string vacancyrequiredskill { getset; }    
    35. }  
    Career.cshtml
    1. @model projectname.Models.ViewModel  
    2. class="container">    
    3.     class="row">    
    4.         class="col-md-6">    
    5.             "margin-top:150px;">Current Job Openings    
    6.         
        
  •         
  •     
  •     
  • class="container">    
  •     "font-size:18px;">    
  •             
  •             
  •     
  •                 
  •     
  •             
  •     
  •     
  •             @foreach (var carr in Model.Vacancy)    
  •             {    
  •                 
  •     
  •                     
  •     
  •     
  •                 
  •     
  •             }    
  •     
  •             
  •     
  •                 
  •     
  •             
  •     
  •     
  •             @foreach (var carr in Model.Vacancy)    
  •             {    
  •                 
  •     
  •                     
  •     
  •     
  •                 
  •     
  •             }    
  •     
  •             
  •     
  •                 
  •     
  •             
  •     
  •     
  •             @foreach (var carr in Model.Vacancy)    
  •             {    
  •                 
  •     
  •                     
  •     
  •     
  •                 
  •     
  •             }    
  •     
  •             
  •     
  •                 
  •     
  •             
  •     
  •     
  •             @foreach (var carr in Model.Vacancy)    
  •             {    
  •                 
  •     
  •                     @{    
  •                         //here I am removing the dots and add the glyphiicon //split means remove the character    
  •                         var dataJobDescription = @carr.vacancyjobdescription.Split('.');    
  •                     }    
  •                     
  •     
  •                 
  •     
  •             }    
  •     
  •             
  •     
  •                 
  •     
  •             
  •     
  •     
  •             @foreach (var carr in Model.Vacancy)    
  •             {    
  •                 
  •     
  •                     @{    
  •                         //here I am removing the dots and add the glyphiicon //split means remove the character    
  •                         var dataRequiredSkill = @carr.vacancyrequiredskill.Split('.');    
  •                     }    
  •                     
  •     
  •                 
  •     
  •             }    
  •     
  •             
  •     
  •                 
  •     
  •             
  •     
  •         
  • Vacancy Title
        
  •                         -    
  •                         @carr.vacancytitle    
  •                     
  • Vacancy Position
        
  •                         -    
  •                         @carr.vacancyposition    
  •                     
  • Vacancy Experiance
        
  •                         -    
  •                         @carr.vacancyexperience    
  •                     
  • Vacancy Job Description
        
  •                         @for (var i = 0; i < dataJobDescription.Length; i++)    
  •                         {    
  •                                 
  •                                 "~/bootstrap-icons-1.0.0/chevron-right.svg" width="20" height="20" />    
  •                                 
  •                             @dataJobDescription[i]    
  •                                 
  •                         }    
  •                     
  • Vacancy Required Skill
        
  •                         @for (var i = 0; i < dataRequiredSkill.Length; i++)    
  •                         {    
  •                                 
  •                                 "~/bootstrap-icons-1.0.0/chevron-right.svg" width="20" height="20" />    
  •                                 
  •                             @dataRequiredSkill[i]    
  •                                 
  •                         }    
  •                     
  •     
  •                     class="btn btn-success applylink">Apply    
  •                 
  •     
  •         
  •    
  • HomeController.cs
    1. [HttpGet]     
    2.  public async Task Career(List _vacancy)    
    3.  {    
    4.      //some code    
    5.      HttpResponseMessage responseMessage = client.GetAsync("https://localhost:44325/Home/DisplayVacancyData").Result; //here I am fetching admin side vacancy to client side    
    6.     
    7.      if (responseMessage.IsSuccessStatusCode)    
    8.      {    
    9.          //some code    
    10.     
    11.          if (_vacancy.Count == 0)    
    12.          {    
    13.              ViewBag.message = "No content Available"//if no vacancy avilable in admin side then display client side No content Available    
    14.          }    
    15.          else    
    16.          {    
    17.              return View("Career", test);     
    18.          }    
    19.      }    
    20.      return View("Index");    
    21.  }  
    problem is here
    1. @carr.vacancytitle //problem is here all the data get here need to implement logic  
    2. @carr.vacancyposition //problem is here all the data get here need to implement logic  
    3. @carr.vacancyexperience //problem is here all the data get here need to implement logic  
    which place to add foreach loop because data is dynamic here can not use for loop
     
    here need to implement foreach loop because data is dynamic not static
     
    please help

    7 Replies

    Know the answer? Post it — somebody with the same question will find it here.