-------

-------

  • NA
  • 213
  • 6.2k

Implement foreachloop in mvc

Sep 29 2020 8:56 AM
I am fetching the admin side vacancy in client side
 
currenty admin side three vacancy available and I am displaying three vacancy in client side
 
In client side display the three vacancy
 
problem is there I want to display each vacancy display separate
 
currently all vacancy display together that is main problem
 
if no vacancy available admin side then display client side no content available
 
for example:
 
what I want to:
  1. <div class="row">    
  2.     <div class="col-md-6">    
  3.         //first vacancy data and apply button    
  4.     </div>    
  5.     <div class="col-md-6">    
  6.         //second vacancy data and apply button    
  7.     </div>    
  8. </div>  
  9. <div class="row">    
  10.     <div class="col-md-6">    
  11.         //third vacancy data and apply button    
  12.     </div>    
  13.     <div class="col-md-6">    
  14.         //fourth vacancy data and apply button    
  15.     </div>    
  16. </div>    
if no vacancy admin side then below display image
  1. <div class="row">  
  2. //@ViewBag.message //here I am fetching controller viewbag message in model means **no content available**  
  3. </div>  
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> 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. <div class="container">    
  3.     <div class="row">    
  4.         <div class="col-md-6">    
  5.             <h2 style="margin-top:150px;">Current Job Openings</h2>    
  6.         </div>    
  7.     </div>    
  8. </div>    
  9.     
  10. <div class="container">    
  11.     <div style="font-size:18px;">    
  12.         <table>    
  13.             <tr>    
  14.                 <th>Vacancy Title</th>    
  15.             </tr>    
  16.     
  17.             @foreach (var carr in Model.Vacancy)    
  18.             {    
  19.                 <tr>    
  20.                     <td>    
  21.                         <text>-</text>    
  22.                         @carr.vacancytitle    
  23.                     </td>    
  24.     
  25.                 </tr>    
  26.             }    
  27.     
  28.             <tr>    
  29.                 <th>Vacancy Position</th>    
  30.             </tr>    
  31.     
  32.             @foreach (var carr in Model.Vacancy)    
  33.             {    
  34.                 <tr>    
  35.                     <td>    
  36.                         <text>-</text>    
  37.                         @carr.vacancyposition    
  38.                     </td>    
  39.     
  40.                 </tr>    
  41.             }    
  42.     
  43.             <tr>    
  44.                 <th>Vacancy Experiance</th>    
  45.             </tr>    
  46.     
  47.             @foreach (var carr in Model.Vacancy)    
  48.             {    
  49.                 <tr>    
  50.                     <td>    
  51.                         <text>-</text>    
  52.                         @carr.vacancyexperience    
  53.                     </td>    
  54.     
  55.                 </tr>    
  56.             }    
  57.     
  58.             <tr>    
  59.                 <th>Vacancy Job Description</th>    
  60.             </tr>    
  61.     
  62.             @foreach (var carr in Model.Vacancy)    
  63.             {    
  64.                 <tr>    
  65.                     @{    
  66.                         //here I am removing the dots and add the glyphiicon //split means remove the character    
  67.                         var dataJobDescription = @carr.vacancyjobdescription.Split('.');    
  68.                     }    
  69.                     <td>    
  70.                         @for (var i = 0; i < dataJobDescription.Length; i++)    
  71.                         {    
  72.                             <span>    
  73.                                 <img src="~/bootstrap-icons-1.0.0/chevron-right.svg" width="20" height="20" />    
  74.                             </span>    
  75.                             @dataJobDescription[i]    
  76.                             <br />    
  77.                         }    
  78.                     </td>    
  79.                 </tr>    
  80.             }    
  81.     
  82.             <tr>    
  83.                 <th>Vacancy Required Skill</th>    
  84.             </tr>    
  85.     
  86.             @foreach (var carr in Model.Vacancy)    
  87.             {    
  88.                 <tr>    
  89.                     @{    
  90.                         //here I am removing the dots and add the glyphiicon //split means remove the character    
  91.                         var dataRequiredSkill = @carr.vacancyrequiredskill.Split('.');    
  92.                     }    
  93.                     <td>    
  94.                         @for (var i = 0; i < dataRequiredSkill.Length; i++)    
  95.                         {    
  96.                             <span>    
  97.                                 <img src="~/bootstrap-icons-1.0.0/chevron-right.svg" width="20" height="20" />    
  98.                             </span>    
  99.                             @dataRequiredSkill[i]    
  100.                             <br />    
  101.                         }    
  102.                     </td>    
  103.                 </tr>    
  104.             }    
  105.     
  106.             <tr>    
  107.                 <td>    
  108.                     <button class="btn btn-success applylink">Apply</button>    
  109.                 </td>    
  110.             </tr>    
  111.         </table>    
  112.     </div>    
  113. </div>   
HomeController.cs
  1. [HttpGet]     
  2.  public async Task<ActionResult> Career(List<Vacancy> _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

Answers (7)