在我的MainView中,我能够使用_GetForTask局部视图渲染所有“TaskComment”(模型)。然后在我的_GetForTask局部视图中,我能够渲染_TaskCommentForm并成功传递了一个新的TaskComment模型实例。但是,每当我单击_TaskCommentForm局部视图中的“提交”按钮时,它总是返回null。我在这里错过了什么?非常感谢。
“TaskComment”模型
public partial class TaskComment
{
public int TaskCommentID { get; set; }
public Nullable<int> TaskID { get; set; }
public Nullable<int> AuthorID { get; set; }
public string Comment { get; set; }
public Nullable<System.DateTime> TimeStamp { get; set; }
public virtual Task Task { get; set; }
public virtual Employee Employee { get; set; }
}
这是我的“MainView”
@Html.Action("_GetForTask", "TaskComment", new { TaskID = Model.TaskID })
_GetForTask PartialView
@model IEnumerable<ProjectYesha.Models.TaskComment>
<div id="comments">
<ul>
@foreach (var taskComment in Model)
{
<li>@taskComment.Comment</li>
}
</ul>
@using (Html.BeginForm("_Submit", "TaskComment", FormMethod.Post))
{
@Html.AntiForgeryToken()
@Html.Action("_TaskCommentForm", "TaskComment", new { TaskID = ViewBag.TaskID })
}
</div>
_TaskCommentForm PartialView
@model ProjectYesha.Models.TaskComment
@Html.HiddenFor(m=> m.TaskCommentID)
@Html.HiddenFor(m=> m.TaskID)
@Html.HiddenFor(m=> m.AuthorID)
@Html.HiddenFor(m=> m.TimeStamp)
@Html.HiddenFor(m=> m.Employee)
@Html.HiddenFor(m=> m.Task)
<div>
@using (Html.BeginForm())
{
@Html.EditorFor(m => m.TaskID)
@Html.EditorFor(m => m.Comment)
<input type="submit" value = "Submit" />
}
</div>
TaskCommentController
public PartialViewResult _GetForTask(Int32 taskID)
{
ViewBag.TaskID = taskID;
List<TaskComment> comments = db.TaskComments.Where(c => c.TaskID == taskID).ToList();
return PartialView("_GetForTask", comments);
}
[ChildActionOnly()]
public PartialViewResult _TaskCommentForm(Int32 taskID)
{
TaskComment comment = new TaskComment()
{
TaskID = taskID ,
};
return PartialView("_TaskCommentForm", comment);
}
[HttpPost]
[ValidateAntiForgeryToken()]
public PartialViewResult _Submit(TaskComment comment)
{
db.TaskComments.Add(comment);
db.SaveChanges();
List<TaskComment> comments = db.TaskComments.Where(c => c.TaskID == comment.TaskID).ToList();
ViewBag.TaskID = comment.TaskID;
return PartialView("_GetForTask", comments);
}