Я создаю простой блог.Когда пользователь добавляет комментарий к определенной записи, детали комментария не связываются при выдаче запроса на публикацию?
И когда я выбрасываю точку останова и метод сообщения. Все параметры имеют «Красный крестик» и.
. Вот весь код.
Контроллер * 1012.*
using Blog.Models;
using Blog.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.Entity;
namespace Blog.Controllers
{
public class CommentsController : Controller
{
private ApplicationDbContext _context;
public CommentsController()
{
_context = new ApplicationDbContext();
}
// GET: Comments
public ActionResult Index(int entryId)
{
Entry entryFromDb = _context.Entries.Include(c => c.Category).Single(e => e.Id == entryId);
EntryAndCommentViewModel viewModel = new EntryAndCommentViewModel()
{
entry = entryFromDb,
EntryId = entryId
};
return View("CommentForm",viewModel);
}
[HttpPost]
public ActionResult Save(Comment comment)
{
Entry entryFromDb = _context.Entries.Include(c => c.Category).Single(e => e.Id == comment.EntryId);
if (!ModelState.IsValid)
{
EntryAndCommentViewModel _viewModel = new EntryAndCommentViewModel()
{
entry = entryFromDb,
EntryId = comment.EntryId
};
return View("CommentForm", _viewModel);
}
return View();
}
}
}
ViewModel
using Blog.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace Blog.ViewModels
{
public class EntryAndCommentViewModel
{
[Required]
public int EntryId { get; set; }
public Entry entry { get; set; }
[Required]
[StringLength(50)]
public string Name { get; set; }
[Required]
[StringLength(100)]
[EmailAddress]
public string EmailAddress { get; set; }
[Required]
[StringLength(1000)]
public string CommentDetails { get; set; }
}
}
Просмотр
@model Blog.ViewModels.EntryAndCommentViewModel
@{
ViewBag.Title = "CommentForm";
}
<br />
<p class="alert alert-danger">Add <b>Comment</b> to this Post.</p>
<h3>
<b>
@Model.entry.Subject
</b>
</h3>
<p>
<i>
Category : <b>@Model.entry.Category.Name</b> - Posted on @Model.entry.PostDate.ToString("dd MMM yyyy")
</i>
</p>
<p>
@Model.entry.Body
</p>
<P>
(0) Comments
</P>
<br />
<h2>Leave a Comment</h2>
<br />
@using (Html.BeginForm("Save", "Comments"))
{
@Html.HiddenFor(m => m.entry.Id)
<div class="form-group">
@Html.LabelFor(m => m.Name)
@Html.TextBoxFor(m => m.Name, new { @class = "form-control", autofocus = "autofocus", Placeholder = "e.g James Anderson" })
@Html.ValidationMessageFor(m => m.Name)
</div>
<div class="form-group">
@Html.LabelFor(m => m.EmailAddress)
@Html.TextBoxFor(m => m.EmailAddress, new { @class = "form-control", Placeholder = "e.g YourEmail@Domain.com" })
@Html.ValidationMessageFor(m => m.EmailAddress)
</div>
<div class="form-group">
@Html.LabelFor(m => m.CommentDetails)
@Html.TextAreaFor(m => m.CommentDetails, 4, 8, new { @class = "form-control", Placeholder = "e.g Comment description goes here!" })
@Html.ValidationMessageFor(m => m.CommentDetails)
</div>
<button type="submit" class="btn btn-primary">Save</button>
<button type="reset" class="btn btn-link">Reset</button>
}
Как это исправить?Спасибо