在按下提交之前,我收到了textboxfor string字段的验证消息,如何解决此问题? [英] I receive a validation message for textboxfor string field before pressing submit, how to fix this?
问题描述
大家好。我正在研究一个c#mvc应用程序。我在ViewModel中验证字符串属性为Required。以下是来自ViewModel的代码:
[必填(ErrorMessage =请输入数字。)]
[RegularExpression( ^ [0-9] * $,ErrorMessage =请只输入一个数字而不是另一个符号。)]
public string BoardNumber {get;组; }
以下是视图中的代码:
< table style =background-color:rgba(224,95,3,0.18); border-radius:8px;对齐= 中心 >
< tr>
< td>
BoardNumber:
< p style =margin-left:20px; margin-right:20px;>
@ Html.TextBoxFor(x => x.BoardNumber,new {@class =form-control,@ Value =})< br />
@ Html.ValidationMessageFor(x => x.BoardNumber,,new {@class =bg-warning})
@ Html.ValidationMessage(error,,new { @class =bg-warning})
< / p>
< / td>
< td>
< p>
< input type =submitstyle =margin-left:20px; margin-right:20px; margin-top:10px; value =提交class =btn btn-primary orders-search-button>
< / p>
< / td>
< / tr>
< / table>
问题是当我转到视图时,甚至会显示所需的错误消息虽然我没有按提交,但我希望只有当我按提交并且没有输入任何内容时才显示它。我该怎么办呢?
我尝试过的事情:
我没有尝试任何东西,因为我不知道该怎么做。
,ErrorMessage =请只输入一个不是另一个标志的数字。)]
公共字符串BoardNumber {get; set;}
以下是视图中的代码:
< table style =background-color:rgba(224,95,3,0.18); border-radius:8px;align =center>
< tr>
< td>
BoardNumber:
< p style =margin-left: 20像素; margin-right:20px;>
@ Html.TextBoxFor(x => x.BoardNumber,new {@class =form-control,@ Value =})< br />
@ Html.ValidationMessageFor(x => x.BoardNumber,,new {@class =bg-warning})
@ Html.ValidationMessage(error,,new { @class =bg-warning})
< / p>
< / td>
< td>
< p>
< input type =submitstyle =margin-left:20px;保证金权:20px的; margin-top:10px;value =提交class =btn btn-primary orders-search-button>
< / p>
< / td>
< ; / tr>
< / table>
问题是当我转到视图时,所需的错误即使我没有按提交,也会显示消息,我希望只有当我按下提交并且没有输入任何内容时才会显示。我该如何解决?
我尝试了什么:
我没有尝试任何东西,因为我不知道该怎么做。
添加两种不同的方法来查看和发布这样的表格
public ActionResult CreateSomething()
{
返回视图();
}
[HttpPost]
公共ActionResult CreateSomething(Models.ModelName ModelName)
{
}
Hi, everyone. I am working on a c# mvc application. I validate a string property as Required in a ViewModel. Here is my code from the ViewModel:
[Required (ErrorMessage = "Please enter a number.")]
[RegularExpression("^[0-9]*$", ErrorMessage = "Please enter only a number not another sign.")]
public string BoardNumber { get; set; }
Here is the code from the View:
<table style="background-color:rgba(224, 95, 3, 0.18);border-radius:8px;" align="center"> <tr> <td> BoardNumber: <p style="margin-left: 20px; margin-right:20px;"> @Html.TextBoxFor(x => x.BoardNumber, new { @class = "form-control" , @Value = "" }) <br /> @Html.ValidationMessageFor(x => x.BoardNumber, "", new { @class = "bg-warning" }) @Html.ValidationMessage("error", "", new { @class = "bg-warning" }) </p> </td> <td> <p> <input type="submit" style="margin-left: 20px; margin-right:20px; margin-top: 10px;" value="Submit" class="btn btn-primary orders-search-button"> </p> </td> </tr> </table>
The problem is that when I go to the view, the required error message is shown even though I haven't pressed submit, and I want it to be displayed only if I press Submit and haven't entered anything. How should I fix that?
What I have tried:
I haven't tried anything because I don't know what to do.
", ErrorMessage = "Please enter only a number not another sign.")]
public string BoardNumber { get; set; }
Here is the code from the View:
<table style="background-color:rgba(224, 95, 3, 0.18);border-radius:8px;" align="center"> <tr> <td> BoardNumber: <p style="margin-left: 20px; margin-right:20px;"> @Html.TextBoxFor(x => x.BoardNumber, new { @class = "form-control" , @Value = "" }) <br /> @Html.ValidationMessageFor(x => x.BoardNumber, "", new { @class = "bg-warning" }) @Html.ValidationMessage("error", "", new { @class = "bg-warning" }) </p> </td> <td> <p> <input type="submit" style="margin-left: 20px; margin-right:20px; margin-top: 10px;" value="Submit" class="btn btn-primary orders-search-button"> </p> </td> </tr> </table>
The problem is that when I go to the view, the required error message is shown even though I haven't pressed submit, and I want it to be displayed only if I press Submit and haven't entered anything. How should I fix that?
What I have tried:
I haven't tried anything because I don't know what to do.
add two different methods for viewing and posting form like this
public ActionResult CreateSomething()
{
return view();
}
[HttpPost]
public ActionResult CreateSomething(Models.ModelName ModelName)
{
}
这篇关于在按下提交之前,我收到了textboxfor string字段的验证消息,如何解决此问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!