Click here to Skip to main content
       

.NET Framework

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questionpulling across value from list to display in viewmemberxnaLearner14 Dec '12 - 5:58 
Hey Guys...so trying to get my head around MVC
 
My Index for holiday page is linked to the model
 
@model HolidayBookingApp.Models.HolidayList
Im trying to pull the value of Holidays remaining from the HList4DD (HolidayListForDropDown) which is coming from the HolidayList.cs page.
 
I can get it through...
 
@foreach (var item in Model.HList4DD)
{
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.PersonId)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.HolidayDate)
        </td>
        <td>
        @Html.DisplayFor(modelItem => item.Person.HolidaysRemaining)
        </td>
Although this displays the holidays remaining for every holiday booked. So if 20 holidays is the limit (4 booked and 16 remaining) this will produce a list of the dates of holidays booked i.e 4 records stating which date is booked, but it will show the '16' holidays remaining 4 times as well.
 
Yes, this is because it is inside the for loop but I tried to display it on its own and it didnt work
 
@Html.DisplayFor(model => model.HList4DD.//what do i add here?
Also tried to add
 
Person person = new Person();
            currentHolidaysRemaining = person.HolidaysRemaining; 
to the HolidayList. cs then in the view do
@Html.DisplayFor(model => model.currentHolidaysRemaining)
but its not pulling across the correct value, just returning 0
 
please advise....thanks
AnswerRe: pulling across value from list to display in viewmemberxnaLearner14 Dec '12 - 6:49 
Im now using
bool isFirst = true;
@int remainingHolidays = 0;
 
@foreach (var item in Model.HList4DD)
{
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.PersonId)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.HolidayDate)
        </td>
   
 
   if (isFirst)
   {
      remainingHolidays = item.Person.HolidaysRemaining;
      isFirst = false;
   }
        
        <td>
            @Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
            @Html.ActionLink("Details", "Details", new { id = item.Id }) |
            @Html.ActionLink("Delete", "Delete", new { id = item.Id })
        </td>
    </tr>}
        
    
    <tr>
      <div class="editor-label">
         @*   @Html.LabelFor(model => model.PList4DD, "Person")*@
        </div>
 
        <div class="editor-field">
       
             <form action ="/Holidays/Index" id="some" method="post"> 
 
            @* *WORKING VERSION**@ @Html.DropDownListFor(model => model.HList4DD.First().HolidayDate, new SelectList(Model.PList4DD, "Id", "Name", Model.currentPersonID), "--select--")
        
 
       <script>
           function updateFormEnabled() 
           {
               if (verifyAdSettings()) 
               {
                   $('#sbmt').removeAttr('disabled');
               }
               else 
               {
                   $('#sbmt').attr('disabled', 'disabled');
               }
           }
 
           function verifyAdSettings() 
           {
               if ($('#HolidayDate').val() != '') 
               {
                   return true;
 
               }
               else 
               {
                   return false;
               }
           }
 

           $('#HolidayDate').change(updateFormEnabled);         
           </script>
 
      <td>
      @remainingHolidays.ToString()
   </td>
               
             <input type="submit" id= "sbmt" name="ViewHolidaysDD" value="View"/>
              </form>
         
  <script>
      $('#sbmt').attr('disabled', '');
        </script>
 
</table>
 

the @remainingHolidays.ToString() 5 lines or so up from the bottom is saying it doesnt exist in the current context?
AnswerRe: pulling across value from list to display in viewmemberfrazzle-me14 Dec '12 - 17:41 
Maybe you are over thinking the problem.
 
xnaLearner wrote:
@foreach (var item in Model.HList4DD)
{
<!-- add a variable say int i = 0;-->
<tr>
<td>
@Html.DisplayFor(modelItem => item.PersonId)
</td>
<td>
@Html.DisplayFor(modelItem => item.HolidayDate)
</td>
<!-- if i == 0 -->
<td>
@Html.DisplayFor(modelItem => item.Person.HolidaysRemaining)
</td>

 
Then you just increment the value of i.
 
Hope this helps.
 
[edit] fixed formating.
Frazzle the name say's it all

Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
John F. Woods

GeneralRe: pulling across value from list to display in viewmemberxnaLearner15 Dec '12 - 8:28 
Yeah that worked thanks for the help frazzle

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   


Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 24 May 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid