c# - ID始终无效

标签 c# asp.net-core asp.net-core-mvc

我创建了一个博客页面,我想向我的博客页面添加评论。我创建了一个名为 BlogComment 的模型和 Controller ,并在我的 Blog 页面上打开了属于 BlogCommentCreate 操作表单.

当我发布表单时,我收到 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);
}

最佳答案

我尝试使用您的代码并收到 ModelState 错误: enter image description here

然后我给输入框设置了默认值0

<input name="RefId" id="RefId" type="number" value="0" hidden>

错误消失了 enter image description here 似乎是 ModelBidng 错误

如果您仍然遇到相同的 ModelState 错误,请输入 document可能有帮助

关于c# - ID始终无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75180743/

相关文章:

c# - VS2008 - 从 Web 服务反序列化时出错

asp.net-core - EF 7 .NET Core 工具错误

error-handling - 在错误处理操作中如何确定我应该返回 web View 还是 api 响应

c# - RIA 服务 WCF 超时

c# - 在 setter 中获取属性名称

c# - 将多种内容类型写入 Web 部件页面响应流

c# - 将嵌套对象发送到asp.net core Get方法

c# - 500 从 .net core 2.1 调用标量数据库函数时出错

c# - 将文件上传到 Azure 上托管的 ASP.NET Core Web 应用程序的目录

asp.net-core-mvc - 在 Controller 中添加 session 值