我正在开发dot Net + share-point的项目, 在那里控制器得到共享点列表记录&创建列表对象列表。 在视图中将对象捕获为一个模型并在表格中进行稀释。
表视图 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"/>
}
我正在尝试将表格数据输入控制器..帮助我,我的代码只需要几个小时,需要获取项目名称日期&amp;小时在一起。
我的控制器。
[HttpPost]
public ActionResult Index(TimeTracking timeTracking)
{
////do the logic here
return View(this.ReceiveTimeTrackingData());
}
我使用了两种模式 - : 型号1
public class TimeTrackingDetails
{
public string ProjectName { get; set; }
public DateTime Date { get; set; }
public double Hours { get; set; }
}
模型2 - :
public class TimeTracking
{
public List<List<TimeTrackingDetails>> TimeTrakings { get; set; }
}
答案 0 :(得分:1)
如果您需要项目名称,则应创建input
。现在在您的代码中,您只有这一行的纯文本:
@Model.TimeTrakings[i][0].ProjectName
只需在其上添加另一行:
@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)