我是C#和MVC的新手,我遇到了传递更新的问题。我有许多视图循环的项目,有些项目的值没有填写(开始和完成日期)。我可以获取要显示的数据但是当我尝试将已更新的项目传递回模型以进行更新时失败,因为它实际上没有将项目传回。如何让按钮将数据从视图传递回控制器。这是模型:
public partial class ProjectTasks
{
public int Project_ID { get; set; }
public int Task_ID { get; set; }
public string DKey { get; set; }
public string Description { get; set; }
public Nullable<bool> FirstTask { get; set; }
public Nullable<int> PlannedDuration { get; set; }
public Nullable<int> Team_ID { get; set; }
public Nullable<System.DateTime> EarlyStartDate { get; set; }
public Nullable<System.DateTime> EarlyFinishDate { get; set; }
public Nullable<System.DateTime> LateStartDate { get; set; }
public Nullable<System.DateTime> LateFinishDate { get; set; }
public Nullable<System.DateTime> PlannedStartDate { get; set; }
public Nullable<System.DateTime> ActualStartDate { get; set; }
public Nullable<System.DateTime> ActualFinishDate { get; set; }
public string AssignedUser_ID { get; set; }
public string Completed_By { get; set; }
public Nullable<bool> DurationEdit { get; set; }
public long ID { get; set; }
public virtual Teams Teams { get; set; }
public virtual Users Users_CompletedBy { get; set; }
public virtual Users Users_AssignedUser { get; set; }
public virtual Projects Projects { get; set; }
}
控制器:
public ActionResult viewTasks(int ProjectID)
{
var apt_projecttasks = from s in db.APT_ProjectTasks
where s.Project_ID.Equals(ProjectID)
select s;
//APT_ProjectTasks projecttasks = db.APT_ProjectTasks.FirstOrDefault(p => p.Project_ID == ProjectID);
if (apt_projecttasks == null)
{
return HttpNotFound();
}
return View(apt_projecttasks);
}
[HttpPost]
public ActionResult viewTasks(ProjectTasks projecttasks)
{
var ProjID = projecttasks.Project_ID;
var test = projecttasks.Description;
db.Entry(projecttasks).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("viewTasks","ProjectTasks", new { ProjectID=ProjID });
}
查看:
@model IEnumerable<APTII_MVC.Models.ProjectTasks>
@{
ViewBag.Title = "viewTasks";
}
<h2>Tasks For Project @Model.First().Project_ID - @Model.First().Projects.EmployerRef</h2>
<table>
<tr>
<th>Task</th>
<th>Team</th>
<th>Duration</th>
<th>Estimated Start</th>
<th>Estimated Finish</th>
<th>Actual Start</th>
<th>Actual Finish</th>
<th>Assigned/Completed</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>@Html.DisplayFor(modelItem => item.Description) </td>
<td>@Html.DisplayFor(modelItem => item.Teams.Description )</td>
<td>@Html.DisplayFor(modelItem => item.PlannedDuration )</td>
<td>@item.EarlyStartDate.Value.ToShortDateString()</td>
<td>@item.EarlyFinishDate.Value.ToShortDateString()</td>
@if (item.ActualStartDate.HasValue)
{
<td>@item.ActualStartDate.Value.ToShortDateString()</td>
}
else
{
<td>@Html.TextBoxFor(modelItem => item.ActualStartDate,
new { @style = "Width:123px", @type = "date", @Value = "" })</td>
}
@if (item.ActualFinishDate.HasValue)
{
<td>@item.ActualFinishDate.Value.ToShortDateString()</td>
}
else
{
<td>@Html.TextBoxFor(modelItem => item.ActualFinishDate,
new { @style ="width:123px", @type = "date", @Value = "" })</td>
}
@if (item.Completed_By != "")
{
<td>@Html.DisplayFor(modelItem => item.Users_CompletedBy.LastName), @Html.DisplayFor(modelItem => item.Users_CompletedBy.FirstName)</td>
}
else if (item.AssignedUser_ID != "")
{
<td>@Html.DisplayFor(modelItem => item.Users_AssignedUser.LastName ), @Html.DisplayFor(ModelItem => item.Users_AssignedUser.FirstName) </td>
}
else {
<td>None Assigned</td>
}
<td>
@if (!item.ActualFinishDate.HasValue || !item.ActualStartDate.HasValue)
{
using(Html.BeginForm("viewTasks","ProjectTasks",FormMethod.Post))
{
<input type="submit" value="Update" />
}
}
</td>
<td>@Html.HiddenFor(modelitem => item.ID)</td>
</tr>
}
</table>
答案 0 :(得分:0)
@foreach (var item in Model)
{
<tr>
@using(Html.BeginForm("viewTasks","ProjectTasks",FormMethod.Post))
{
<td>@Html.DisplayFor(modelItem => item.Description) </td>
<td>@Html.DisplayFor(modelItem => item.Teams.Description )</td>
<td>@Html.DisplayFor(modelItem => item.PlannedDuration )</td>
<td>@item.EarlyStartDate.Value.ToShortDateString()</td>
<td>@item.EarlyFinishDate.Value.ToShortDateString()</td>
@if (item.ActualStartDate.HasValue)
{
<td>@item.ActualStartDate.Value.ToShortDateString()</td>
}
else
{
<td>@Html.TextBoxFor(modelItem => item.ActualStartDate,
new { @style = "Width:123px", @type = "date", @Value = "" })</td>
}
@if (item.ActualFinishDate.HasValue)
{
<td>@item.ActualFinishDate.Value.ToShortDateString()</td>
}
else
{
<td>@Html.TextBoxFor(modelItem => item.ActualFinishDate,
new { @style ="width:123px", @type = "date", @Value = "" })</td>
}
@if (item.Completed_By != "")
{
<td>@Html.DisplayFor(modelItem => item.Users_CompletedBy.LastName), @Html.DisplayFor(modelItem => item.Users_CompletedBy.FirstName)</td>
}
else if (item.AssignedUser_ID != "")
{
<td>@Html.DisplayFor(modelItem => item.Users_AssignedUser.LastName ), @Html.DisplayFor(ModelItem => item.Users_AssignedUser.FirstName) </td>
}
else {
<td>None Assigned</td>
}
<td>
@if (!item.ActualFinishDate.HasValue || !item.ActualStartDate.HasValue)
{
<input type="submit" value="Update" />
}
</td>
<td>@Html.HiddenFor(modelitem => item.ID)</td>
}
</tr>
}
你应该更改名称&#39;控制器方法中绑定的属性。输入名称和类属性名称必须相同:
例如:<input name="ActualFinishDate" >
对应属性&#34; ActualFinishDate&#34;来自ProjectTasks类。
关于绑定:https://www.codeproject.com/articles/710776/introduction-to-asp-net-mvc-model-binding-an-absol