0

I am working on a project in dot Net + share-point, In there controller get sharepoint list record & create a list of list object. In view catch the object as one model and dilplay in a table.

Table view enter image description here

Index.cshtml

@using( Html.BeginForm() )
{
<table id = "timeTrackingView" border="1" >

    <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>
        }

    </tr>
   @{ double[] totalForToday = new double[@Model.DateRange];}

      @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++)
            {
                totalForToday[index] = totalForToday[index] + @Model.TimeTrakings[i][j].Hours;
                 var time = @Html.EditorFor(model => @Model.TimeTrakings[i][j]);
                <td>@time</td>
              @*@Html.EditorFor(model => @Model.TimeTrakings[i][j].Hours)*@ 

                index++;  
            }

        </tr>
    }

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

    </tr>    
</table>
<input type="submit" name="SubmitBtn"/>
}

I am trying to get table data into controller.. help me im my code only gettinh hours, need get project name date & hours together.

My controller.

[HttpPost]
        public ActionResult Index(TimeTracking timeTracking)
        {
            ////do the logic here 
            return View(this.ReceiveTimeTrackingData());
        }

I Used two mode-: Model1

 public class TimeTrackingDetails

        {
             public string ProjectName { get; set; }
            public DateTime Date { get; set; }
            public double Hours { get; set; }        
        }

Model 2-:

public class TimeTracking

    {
              public List<List<TimeTrackingDetails>> TimeTrakings { get; set; }
    }
2
  • 1
    Show you model (you use of EditorFor(model => @Model.TimeTrakings[i][j]) does not really make sense) and you really need to be using nested collections for this Commented Mar 15, 2016 at 7:51
  • You List<List<T>> property is messy and you should consider using a view model with nested collections that better represents what you want to display and does not duplicate data (the ProjectName and Date values). Refer this answer for an example. Commented Mar 15, 2016 at 9:07

1 Answer 1

1

If you need project name you should create an input. Now in your code you have just plain text with this line:

@Model.TimeTrakings[i][0].ProjectName

Just add another line onder it:

@Model.TimeTrakings[i][0].ProjectName
// this line will create additional hidden input and on form post it will be sended to server.
@Html.HiddenFor(x => x.TimeTrakings[i][0].ProjectName) 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, It works for me. I added @Html.HiddenFor(x => x.TimeTrakings[i][0].ProjectName) with each attributes. It will return all the records when submitting form.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.