问题是,-6ren">
gpt4 book ai didi

asp.net - ValidationMessageFor 的问题

转载 作者:太空宇宙 更新时间:2023-11-04 16:30:14 25 4
gpt4 key购买 nike

你好,

我在我的页面上放置了以下代码:

<%: Html.ValidationSummary("Form not correct", new { @class = "errList" })%>

然后在每个属性上我都有类似的东西:

<%: Html.ValidationMessageFor(model => model.ModelViewAd.Title, "*", new { @class = "val" })%>

问题是,只要我打开页面,每个 ValidationMessageFor 都会显示 * 并且 ValidationSummary 会显示“表单不正确”,即使表单尚未经过验证?但是,在验证表单后,将首先显示摘要列表。

我只需要在表单通过验证后才显示 ValidationMessageFor 和 ValidationSummary。

我错过了什么?

Edit1:属性确实有 DataAnnotations。

编辑2:

这是 View 的第一部分的样子

<div id="adRegistrationForm">
<% Html.EnableClientValidation(); %>
<h1>Registrera din annons</h1>
<%: Html.ValidationMessageFor(model => model.ModelViewAd, "*", new { @class = "val" })%>
<br />
<% using (Html.BeginForm("Register", "Ad", FormMethod.Post, new { enctype = "multipart/form-data" }))
{%>
<%: Html.ValidationSummary("Formuläret är inte korrekt ifyllt", new { @class = "errList" })%>

<div class="controlTitle">
<%: Html.LabelFor(model => model.ModelViewAd.Title)%>
<%: Html.ValidationMessageFor(model => model.ModelViewAd.Title, "*", new { @class = "val" })%>
</div>
<div>
<%: Html.TextBoxFor(model => model.ModelViewAd.Title, new { @class = "tb1" })%>
</div><br />

<div class="controlTitle">
<%: Html.LabelFor(model => model.ModelViewAd.TypeOfAd)%>
</div>
<div>
<%: MvcHtmlString.Create(Html.RadioButtonListFor(model => model.ModelViewAd.TypeOfAd)) %>
</div><br />

<div id="divEndDate">
<div class="controlTitle">
<%: Html.LabelFor(model => model.ModelViewAd.EndDate)%>
<%: Html.ValidationMessageFor(model => model.ModelViewAd.EndDate, "*", new { @class = "val" })%>
</div>
<div>
<%: Html.TextBoxFor(model => model.ModelViewAd.EndDate)%>
</div>
</div>

这就是 viewClass 的第一部分的样子:

[PropertiesMustMatchAttribute("P1", "P2", ErrorMessage = "Lösenorden stämmer inte")]
public class ModelViewAdRegister
{
#region Fields
private string _title;
private string _description;
#endregion

[Required(ErrorMessage = "Titel saknas")]
[StringLength(50, MinimumLength = 5, ErrorMessage = "Titeln får vara mellan 5-50 tecken lång")]
[DisplayName("Titel")]
public string Title
{
get { return _title; }
set { _title = value ?? string.Empty; }
}

[Required(ErrorMessage = "Pris saknas")]
[DisplayName("Pris (skr)")]
public Decimal Price { get; set; }

[Required(ErrorMessage = "Annons text saknas")]
[DisplayName("Annons text")]
[StringLength(50000, MinimumLength = 10, ErrorMessage = "Annons texten får vara mellan 10-50000 tecken lång")]
public string Description
{
get { return _description; }
set { _description = value ?? string.Empty; }
}

最佳答案

我已经重现了你的问题。您的问题是您的验证摘要在您的表单中。移动这条线

<%: Html.ValidationSummary("Formuläret är inte korrekt ifyllt", new { @class = "errList" })%>

到这一行之前:

 <% using (Html.BeginForm("Register", "Ad", FormMethod.Post, new { enctype = "multipart/form-data" }))
{%>

一切都应该正常工作!

关于asp.net - ValidationMessageFor 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4797665/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com