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; }
}
EditorFor(model => @Model.TimeTrakings[i][j])does not really make sense) and you really need to be using nested collections for thisList<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 (theProjectNameandDatevalues). Refer this answer for an example.