我创建了一个博客页面,我想向我的博客页面添加评论。我创建了一个名为 BlogComment
的模型和 Controller ,并在我的 Blog
页面上打开了属于 BlogComment
的 Create
操作表单.
当我发布表单时,我收到 RefId 无效且尝试值 = "",如果您能提供帮助,我将不胜感激。
这是我的 HTML 标记:
<form asp-area="onepoint" asp-controller="BlogComments" asp-action="Create">
<div hidden>
<input name="RefId" id="RefId" type="number" hidden>
<input name="BlogId" id="BlogId" type="number" hidden>
<input name="BlogCommentApproved" id="BlogCommentApproved" type="checkbox" value="true" hidden>
<input name="BlogCommentApprovedById" id="BlogCommentApprovedById" type="number" hidden>
<input name="BlogCommentApproveDate" id="BlogCommentApproveDate" type="datetime" hidden>
<input name="CommentCreateDate" id="CommentCreateDate" type="datetime" hidden>
</div>
<div class="row">
<div class="col-md-9">
<div class="form-group">
<textarea class="form-control" name="CommentContent" id="CommentContent" rows="5" placeholder="Yorumunuz..."></textarea>
</div>
<div class="row">
<div class="col-md-4">
<input class="form-control" name="Fullname" id="Fullname" type="text" placeholder="Ad Soyad">
</div>
<div class="col-md-4">
<input class="form-control" name="Email" id="Email" type="text" placeholder="E-Posta">
</div>
<div class="col-md-4">
<input id="SendFeedbackBlog" onclick="SendMessage()" type="submit" class="button-62" value="@localizer["Yorum Gönder"]" style="float:right">
</div>
</div>
</div>
<div class="col-md-3"></div>
</div>
</form>
这是我的 BlogComment
类:
/// <summary>
/// Referans Numarası
/// </summary>
[Key]
[Column(TypeName = "int")]
public int RefId { get; set; }
/// <summary>
/// Blog Id
/// </summary>
public int? BlogId { get; set; }
[ForeignKey("BlogId")]
public virtual Blog Blog { get; set; }
/// <summary>
/// Blog Yorumu Onayla
/// </summary>
public bool BlogCommentApproved { get; set; }
/// <summary>
/// Yorumu Onaylayan Kullanıcı Id'si
/// </summary>
public int? BlogCommentApprovedById { get; set; }
[ForeignKey("BlogApprovedById")]
public virtual User ApprovedBy { get; set; }
/// <summary>
/// Blog Yorumu Onay Tarihi
/// </summary>
public DateTime? BlogCommentApproveDate { get; set; }
/// <summary>
/// Yorumun Yazıldığı Tarih
/// </summary>
public DateTime? CommentCreateDate { get; set; }
/// <summary>
/// Yorum Yazanın İsim ve Soyismi
/// </summary>
public string Fullname { get; set; }
/// <summary>
/// Yorum Yazanın E-mail Adresi
/// </summary>
public string Email { get; set; }
/// <summary>
/// Yazılan Yorum
/// </summary>
public string CommentContent { get; set; }
这是我的BlogCommentController
:
public async Task<IActionResult> Create([Bind("RefId,BlogId,BlogCommentApproved,BlogCommentApprovedById,BlogCommentApproveDate,CommentCreateDate,Fullname,Email,CommentContent")] BlogComment blogComment)
{
if (ModelState.IsValid)
{
_context.Add(blogComment);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
ViewData["BlogId"] = new SelectList(_context.Blogs, "RefId", "RefId", blogComment.BlogId);
return View(blogComment);
}
最佳答案
然后我给输入框设置了默认值0
<input name="RefId" id="RefId" type="number" value="0" hidden>
如果您仍然遇到相同的 ModelState 错误,请输入 document可能有帮助
关于c# - ID始终无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75180743/