问题描述
我遇到了模型状态应用于单个页面上存在的每个表单的问题。例如,如果不满足验证,例如用户未完成必填字段,则在回发后将消息“x 字段是必需的”应用于每个表单。该页面上有两个“不同”(但完全相同)的表单
我正在建立一个评论部分,用户可以在其中为每个评论发表评论,这意味着评论回复表单可以存在 x 次,如果有 10 条评论,那么它将在页面上出现 10 次。
型号
public string FullName { get; set; }
public string Email { get; set; }
public string Comment { get; set; }
查看
@model BlogCommentsviewmodel
// when this is pressed use js to show the form
<button type="button" id="form-toggle-btn" class="btn btn-md btn-primary-filled js-reply-comment-button">
<strong>REPLY <i class="fa fa-reply" aria-hidden="true"></i></strong>
</button>
@foreach (var item in Model.BlogComments)
{
///blog comment markup here
div class="reply-comment-form js-reply-comment-form">
@using (Html.BeginUmbracoForm<BlogCommentSurfaceController>("CreateComment",null,new { id = "reply-form"}))
{
@Html.AntiForgeryToken()
<div class="row">
<div class="col-sm-6">
<div class="form-group">
@Html.LabelFor(m => m.FullName)
@Html.TextBoxFor(m => m.FullName,new { placeholder = "Full Name",@class = "form-control" })
@Html.ValidationMessageFor(m => m.FullName)
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
@Html.LabelFor(m => m.Email)
@Html.TextBoxFor(m => m.Email,new { placeholder = "Email",@class = "form-control" })
@Html.ValidationMessageFor(m => m.Email)
</div>
</div>
<div class="col-sm-12">
<div class="form-group">
@Html.LabelFor(m => m.Comment)
@Html.TextAreaFor(m => m.Comment,new { placeholder = "Message",@class = "form-control",@rows = 7 })
@Html.ValidationMessageFor(m => m.Comment)
</div>
</div>
<button type="submit" id="form-reply-submit" class="btn btn-md btn-primary-filled btn-form-submit">
<strong>POST REPLY</strong>
</button>
</div>
}
}
控制器
[HttpPost]
public ActionResult CreateComment(BlogCommentviewmodel model)
{
if (!ModelState.IsValid || model == null)
return CurrentUmbracopage();
return RedirectToCurrentUmbracopage();
}
表单仅在“发布回复按钮被按下时才显示,我使用了一些带有 CSS 的 JS 来显示\隐藏表单”我遇到的问题,如果验证失败,它适用于页面上的所有表单。这是在客户端使用 jquery 不显眼
如何将其限制为仅与 交互的表单?
我读过这篇感觉很相似Multiple forms in MVC view: ModelState applied to all forms
任何帮助都会很棒。
谢谢
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)