1

I am looking to display some records in table by retrieving from SharePoint list. I used it as @HtmlBeginForm for submitting purpose. In my case there are three buttons,

  • Prev - to preview previous dates records

  • next - to preview next dates records

  • Submit - to update share-point list (after changed table value)

  • when page loading It load data to table from today to last 7 days.

In my case though records retrieved correctly, table will not show updated values. It shows value which had by default loading.

My Table-:

@using (Html.BeginForm())
                {

                    <table  border="1" class="table timetrack-tb">

                        <tr>
                            <th>Projects</th>
                            @for (var date = @Model.Dates.AddDays(-(@Model.DateRange - 1)); date <= @Model.Dates; date = date.AddDays(1))
                            {
                                <th>@date.Day/@date.Month/@date.Year</th>
                            }
                            <th>Total</th>
                        </tr>
                        @{ double[] totalForDay = new double[@Model.DateRange+1];
                         double[] total = new double[@Model.TimeTrakings.Count];}

                        @for (int i = 0; i < @Model.TimeTrakings.Count; i++)
                        {

                            var projectName = @Model.TimeTrakings[i][0].ProjectName;
                            int index = 0;
                            <tr>
                                <td>
                                    @Model.TimeTrakings[i][0].ProjectName
                                </td>
                                @for (int j = 0; j <= Model.TimeTrakings[i].Count(); j++)
                                {
                                    if (j != Model.TimeTrakings[i].Count())
                                   {
                                         totalForDay[index] = totalForDay[index] + @Model.TimeTrakings[i][j].Hours;
                                    total[i] = total[i] + @Model.TimeTrakings[i][j].Hours;   
                                    <td>
                                        @Html.EditorFor(model => @Model.TimeTrakings[i][j].Hours)
                                        @Html.HiddenFor(x => x.TimeTrakings[i][j].Id)
                                        @Html.HiddenFor(x => x.DateRange)
                                        @Html.HiddenFor(x => x.TimeTrakings[i][j].ProjectName)
                                        @Html.HiddenFor(x => x.TimeTrakings[i][j].ProjectCode)
                                        @Html.HiddenFor(x => x.TimeTrakings[i][j].Date)
                                    </td>
                                    index++;
                                    }
                                    else
                                    {
                                        totalForDay[index] = totalForDay[index] + total[i];
                                        index++;
                                    }

                                }
                                <td>@total[i]</td>
                            </tr>
                        }

                        <tr>
                            <td>Total for day</td>
                            @foreach (var tot in totalForDay)
                            {
                                <td>@tot</td>
                            }
                        </tr>
                    </table>

                    <input type="submit" name="prevBtn" value="Prev" />
                    <input type="submit" name="NextBtn" value="Next" />
                    <input  type="submit" name="SubmitBtn"  value="Submit"/>
                }

My Controller method:

public ActionResult Index(TimeTracking timeTracking)
        {
            DateTime today = DateTime.Today;
            int lengthOfTable = timeTracking.DateRange;
            if (Request.Form["SubmitBtn"] != null)
            { 
                timeTrackManager.UpdateTimeTrackingList(timeTracking);
                today = DateTime.Today;
                lengthOfTable = timeTracking.DateRange;
            }
            else if (Request.Form["prevBtn"] != null)
            {
                today = timeTracking.TimeTrakings[0][0].Date.AddDays(-1);
                lengthOfTable = timeTracking.DateRange;

            }
            else if (Request.Form["NextBtn"] != null)
            {
                today = timeTracking.TimeTrakings[0][0].Date.AddDays(timeTracking.DateRange);
                lengthOfTable = timeTracking.DateRange;
            }

            return View(this.ReceiveTimeTrackingData(today, lengthOfTable));
        }
tereško
  • 58,060
  • 25
  • 98
  • 150
RiksonTool
  • 147
  • 1
  • 2
  • 13
  • Only not update cell record in @Html.EditorFor(model => @Model.TimeTrakings[i][j].Hours) element – RiksonTool Mar 18 '16 at 05:53
  • 1
    All you have shown are 3 submit buttons which post to the same controller method - what is the code in your controller method that 'changes' the list based on dates? –  Mar 18 '16 at 06:05
  • 1
    And you continued use of `List>` is going to cause you problems as I noted in your [last question](http://stackoverflow.com/questions/36005258/pass-table-values-from-view-to-controller-mvc)! –  Mar 18 '16 at 06:06
  • when try to display value in just a label, successfully updated. It will not update when use @Html.EditorFor() element, and @Html.HiddenFor() element. I am only changing date for each button submission. when click prev, subtract one day from most 1st column date of table and again retrieve data – RiksonTool Mar 18 '16 at 06:18
  • You have not shown you controller code! And the correct approach is not to submit a form for the 'Next' and 'Previous' values - you should be redirecting to a methods that displays a new view (and passing the dates or just a `int` to indicate the week number) –  Mar 18 '16 at 06:21
  • Have just seen you edit (and its a terrible approach). The reason your form controls are not updated is because the values of you model have been added to `ModelState` (see [this answer](http://stackoverflow.com/questions/26654862/textboxfor-displaying-initial-value-not-the-value-updated-from-code/26664111#26664111) for an explanation), but do not do this anyway. –  Mar 18 '16 at 06:24
  • OK, thank yo so much for your kind help. – RiksonTool Mar 18 '16 at 06:29

0 Answers0