当我尝试在详细信息页面添加评论时,它会发送到此页面:
https://onedrive.live.com/redir?resid=ED946C8D08765D6F%21107
我的控制器:
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Student student = _db.Students.Include(m => m.Comments).SingleOrDefault(x => x.ID == id);
if (student == null)
{
return HttpNotFound();
}
//var enrollments = new Enrollment { };
var model = new DetailsViewModel
{
Comment = new Comment(),
Student = student,
Comments = student.Comments.OrderBy(c => c.Id).ToList()
};
}
public ActionResult AddComment(Comment comment, int commentId)
{
if (ModelState.IsValid && commentId > 0)
{
var commentToAdd = new Comment
{
Email = comment.Email,
CommentId = commentId,
Name = comment.Name,
Text = comment.Text,
};
_db.Comments.Add(commentToAdd);
_db.SaveChanges();
return PartialView("_Comment", commentToAdd);
}
return RedirectToAction("Index", "Student");
}
详细信息视图:
@model ContosoUniversity.ViewModels.DetailsViewModel
@{
ViewBag.Title = "Details";
}
<h2>Details</h2>
<div>
<h4>Student</h4>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.Student.LastName)
</dt>
<dd>
@Html.DisplayFor(model => model.Student.LastName)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Student.FirstMidName)
</dt>
<dd>
@Html.DisplayFor(model => model.Student.FirstMidName)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Student.EnrollmentDate)
</dt>
<dd>
@Html.DisplayFor(model => model.Student.EnrollmentDate)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Student.Enrollments)
</dt>
<dd>
<table class="table">
<tr>
<th>Course Title</th>
<th>Grade</th>
</tr>
</table>
</dd>
</dl>
@using (Html.BeginForm("AddComment", "Student", FormMethod.Get))
{
<div class="form-horizontal">
<h4>Comments</h4>
<hr />
@Html.ValidationSummary(true)
<input type="hidden" value="@Model.Student.ID" name="commentId" />
<div class="form-group">
@Html.LabelFor(model => model.Comment.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Comment.Name)
@Html.ValidationMessageFor(model => model.Comment.Name)
</div>
</div>
@Html.ValidationSummary(true)
<div class="form-group">
@Html.LabelFor(model => model.Comment.Email, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Comment.Email)
@Html.ValidationMessageFor(model => model.Comment.Email)
</div>
</div>
@Html.ValidationSummary(true)
<div class="form-group">
@Html.LabelFor(model => model.Comment.Text, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextAreaFor(model => model.Comment.Text)
@Html.ValidationMessageFor(model => model.Comment.Text)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Add Comment" class="btn btn-default" />
</div>
</div>
</div>
}
<div id="comments">
@foreach (var c in Model.Comments)
{
@Html.Partial("_Comment", c)
}
</div>
</div>
<p>
@Html.ActionLink("Edit", "Edit", new { id = Model.Student.ID }) |
@Html.ActionLink("Back to List", "Index")
</p>
DetailsViewModel:
public Student Student { get; set; }
public List<Comment> Comments { get; set; }
public Comment Comment { get; set; }
评论模型:
public int Id { get; set; }
public int CommentId { get; set; }
public Student Student { get; set; }
[Required]
public string Name { get; set; }
[EmailAddress]
public string Email { get; set; }
[Required]
public string Text { get; set; }
学生模特:
public int ID { get; set; }
public string LastName { get; set; }
public string FirstMidName { get; set; }
public DateTime EnrollmentDate { get; set; }
public List<Comment> Comments { get; set; }
我无法理解它前一段时间的错误。谢谢
答案 0 :(得分:1)
因为那是你告诉它用这条线做的事情
return PartialView("_Comment", commentToAdd);
如果您想返回显示新添加的评论的当前页面,那么它应该是
return RedirectToAction("Details", new { id = commentId });
并且您正在发帖,因此FormMethod.Post
方法
BeginForm()