当我转到insert页面时,我有一个插入页,所有的验证字段都会显示,
[Required(ErrorMessage ="Please Enter Name")]
public string ccname { get; set; }这是我的类,在该类中,我使用所需的验证消息声明字符串ccname。
输入名称
当用户单击insert而没有在ccname中输入数据时,它应该出现。
但是,验证消息显示在页面加载上。
@Html.TextBoxFor(model => model.ccname, new { @class = "textboxstyle" })
@Html.ValidationMessageFor(model => model.ccname)我试了几次,但没什么效果,
下面是一个例子
在我的控制器中我添加了ModelState.clear();
public ActionResult insert()
{
ModelState.Clear();
return View();
}在我看来,我将代码更改为
@Html.TextBoxFor(model => model.ccname, new { @class = "textboxstyle" })
@Html.ValidationMessageFor(model => model.ccname)至
@Html.TextBoxFor(model => model.ccname, new { @class = "textboxstyle" })
@Html.ValidationMessageFor(model => model.ccname,"",new {@style= ".validation-summary-valid { display:none; }" })但这两件事都不管用
我现在该怎么办?
发布于 2017-05-22 07:49:45
示例:
模型:
public class MyModel
{
[Required(ErrorMessage ="Please Enter Name")]
public string ccname { get; set; }
}控制器:
public class HomeController:Controller
{
[HttpGet]
ActionResult Insert()
{
var model =new MyModel();
return View(model);
}
[HttpPost]
[ValidateAntiForgeryToken]
ActionResult Insert(MyModel model)
{
if(ModelState.IsValid)
{
//Do something
return View();
}
return View(model);
}
}视图
Insert.cshtml
@model MyModel
@using (Html.BeginForm("Insert", "Home", FormMethod.Post))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.TextBoxFor(model => model.ccname, new { @class = "textboxstyle" })
@Html.ValidationMessageFor(model => model.ccname)
<input type="submit" value="Insert" class="btn btn-primary" />
}
@Scripts.Render("~/bundles/jqueryval")https://stackoverflow.com/questions/44105687
复制相似问题