我有一个模型,其中包含用户登录信息以及他在本周和下周的任务:
public class TaskThisAndNextWeek
{
public TaskThisAndNextWeek()
{
}
public UserLoginInfo Info { get; set; }
public List<TaskInThisWeek> TaskThisWeek { get; set; }
public List<TaskInNextWeek> TaskNextWeek { get; set; }
public void SetData(UserLoginInfo Info, List<TaskInThisWeek> TaskThisWeek, List<TaskInNextWeek> TaskNextWeek)
{
this.Info = Info;
this.TaskThisWeek = TaskThisWeek;
this.TaskNextWeek = TaskNextWeek;
}
}
然后我将模型发送到控制器UserRountineController.cs中的视图中
public ActionResult ShowTask()
{
//some input data to lists and object inside class TaskThisAndNextWeek
return View(TaskThisAndNextWeek);
}
和HttpPost一样
[HttpPost]
public void UpdateDetail(TaskThisAndNextWeek obj)
{
foreach (var item in obj.TaskThisWeek)
{
System.Diagnostics.Debug.WriteLine(item.JobID);
}
}
我使用循环视图显示数据,并允许用户修改和提交其更改。
@model UserTaskReport.Models.TaskThisAndNextWeek
@using (Html.BeginForm("UpdateDetail", "UserRountine"))
{
@Html.AntiForgeryToken()
.....
for (int i= 0;i<Model.TaskThisWeek.Count() ;i++)
{
<tr class="@(Model.TaskThisWeek[i].isApproved==1?"Company":"")">
<td class="none">@Html.TextBoxFor(m=>m.TaskThisWeek[i].JobID)</td>
<td class="none">@Html.TextBoxFor(m => m.TaskThisWeek[i].Unknown1)</td>
<td class="none">@Html.TextBoxFor(m => m.TaskThisWeek[i].Unknown2)</td>
<td align="center">@Html.TextBoxFor(m => m.TaskThisWeek[i].IniMonth)</td>
<td>@Html.TextBoxFor(m => m.TaskThisWeek[i].userSName)</td>
<td>@Html.TextBoxFor(m => m.TaskThisWeek[i].Title)</td>
<td align="right">@Html.TextBoxFor(m => m.TaskThisWeek[i].lastWeekPercent)</td>
<td align="right">
@Html.TextBoxFor(m => m.TaskThisWeek[i].thisWeekPercent)
@*<input name="thisWeekPercent" type="text" value="@Html.TextBoxFor(m => m.TaskThisWeek[i].thisWeekPercent)" id="" style="text-align: right;">*@
</td>
<td>
@Html.TextBoxFor(m => m.TaskThisWeek[i].thisWeekContent)
@*<input name="thisWeekContent" type="text" id="" value="@item.thisWeekContent">*@
</td>
}
.......
<input type="submit" name="" value="Execute" id="">
}
当我运行程序时,该视图显示了我期望的信息,但是当我进行修改和提交时。发生错误
没有为此对象定义无参数的构造函数。
描述:在执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪,以获取有关错误及其在代码中起源的更多信息。
我试图在模型中将List <>更改为ENumberable,对于视图中的-> foreach甚至将@ Html.TextBox更改为相同的值。 请在这种情况下帮助我。
P / S:我打包了UserInfo进行建模,然后将其推入视图以用于进一步的目的,例如按ID进行搜索...但是我认为这并不安全。有没有发送UserInfo的另一种方法?