在ASP.NET MVC 4项目中,我创建了一个名为Task的模型类及其CRUD,并在索引页面中列出了我需要分别修改每一行所需的任务。所以我在每一行创建一个表单。但是当我提交一行时,我没有在控制器的动作中获得对象任务。
以下是代码:
由于
控制器
public ActionResult Index()
{
ViewBag.projectVersionList = this.getProjectVersionList();
ViewBag.taskTypeList = this.getTaskTypeList();
ViewBag.durationList = this.getDurationList();
return View(db.Tasks.ToList());
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Index(Task task)//task est instancié mais les propriétées sont vides
{
task.User = db.UserProfiles.Find(WebSecurity.CurrentUserId);
if (ModelState.IsValid)
{
db.Entry(task).State = EntityState.Modified;
db.SaveChanges();
}
return View(task);
}
查看
@model IEnumerable<TimeSheet.Models.Task>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<div class="responsive-table-line">
<table class="table table-striped" data-card-view="true">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Date)
</th>
<th>
@Html.DisplayNameFor(model => model.UserId)
</th>
<th>
@Html.DisplayNameFor(model => model.ProjectVersionId)
</th>
<th>
@Html.DisplayNameFor(model => model.TaskTypeId)
</th>
<th>
@Html.DisplayNameFor(model => model.Duration)
</th>
<th>
@Html.DisplayNameFor(model => model.Accomplished)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
@Html.EditorFor(m => item)
}
</tbody>
</table>
</div>
编辑模板:
@model TimeSheet.Models.Task
@using (Ajax.BeginForm(new AjaxOptions
{
HttpMethod = "POST"
}))
{
@Html.AntiForgeryToken()
<tr>
<td data-title="@Html.DisplayNameFor(model => model.Date)">
@Html.HiddenFor(m => m.TaskId)
<div class="form-group">
@Html.TextBoxFor(m => m.Date, new { @class = "datefield form-control", type = "date" })
</div>
</td>
<td data-title="@Html.DisplayNameFor(model => model.UserId)">
@Html.DisplayFor(m => m.User.UserName)
</td>
<td data-title="@Html.DisplayNameFor(model => model.ProjectVersionId)">
<div class="form-group">
@Html.DropDownListFor(m => m.ProjectVersionId, (IEnumerable<SelectListItem>)ViewBag.projectVersionList, new { @class = "form-control" })
</div>
</td>
<td data-title="@Html.DisplayNameFor(model => model.TaskTypeId)">
<div class="form-group">
@Html.DropDownListFor(m => m.TaskTypeId, (IEnumerable<SelectListItem>)ViewBag.taskTypeList, new { @class = "form-control" })
</div>
</td>
<td data-title="@Html.DisplayNameFor(model => model.Duration)">
<div class="form-group">
@Html.DropDownListFor(m => m.Duration, (IEnumerable<SelectListItem>)ViewBag.durationList, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.Duration)
</div>
</td>
<td data-title="@Html.DisplayNameFor(model => model.Accomplished)">
@Html.CheckBoxFor(m => m.Accomplished)
</td>
<td data-title="">
<button type="submit" class="btn btn-default glyphicon glyphicon-floppy-disk" aria-label="Left Align"></button>
@Html.ActionLink(" ", "Edit", new { id = Model.TaskId }, new { @class = "btn btn-default glyphicon glyphicon-edit" })
<button type="button" class="btn btn-default" aria-label="Left Align">
<span class="glyphicon glyphicon-align-left" aria-hidden="true"> @Html.ActionLink(" ", "Details", new { id = Model.TaskId })</span>
</button>
<button type="button" class="btn btn-default" aria-label="Left Align">
<span class="glyphicon glyphicon-trash" aria-hidden="true"> @Html.ActionLink(" ", "Delete", new { id = Model.TaskId })</span>
</button>
</td>
</tr>
}
答案 0 :(得分:0)
谢谢!我找到了pb:每个表单中的字段都是名称&#34; items。[propertyName]&#34;而不是&#34; [propertyName]&#34;。我在模型中替换像这样的
@foreach (var model in Model)
{
@Html.EditorFor(m => model)
}
在EditorTemplace m by model
model => model.Accomplished
谢谢!