在一个 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>
}
谢谢 !我找到了 pb :每个表单中的字段都是名称"项目"。[属性名称]"而不是"[属性名称]"。我像这样用模型替换视图中的项目
@foreach (var model in Model)
{
@Html.EditorFor(m => model)
}
在编辑器中按模型放置m
model => model.Accomplished
谢谢!