即使文本框有数据,必需的验证也会失败
Required validation failing even though text box has data
我有一个搜索页面,它的模型是 MyViewModel
class。由于 [Required]
属性中缺少数据,即使该字段确实具有值,我也遇到了一个字段突出显示为红色的问题。
这是视图模型
public class MyViewModel
{
[DisplayName("My Field")]
[Required]
public string MyField { get; set; }
// This gets populated with search results from the database
// whenever the user clicks the Search button and the page
// posts back
public List<Customer> SearchResults { get; set; }
}
这是 ASP 页面
@model MyProgram.MyViewModel
@using (Html.BeginForm("ListMyData", "Test", FormMethod.Get, htmlAttributes: new { @id = "search-form", @class = "form-horizontal" }))
{
<div class="form-group">
@Html.LabelFor(model => model.MyField, new { @class = "control-label col-sm-2" })
<div class="col-sm-2">
@Html.TextBoxFor(model => model.MyField, htmlAttributes: new { @class = "form-control" })
</div>
</div>
}
这是我的控制器
public class TestController
{
// The first time that the user navigates to this page, the "else" clause
// will execute and the form will get populated with default values. When
// the user clicks the "search" button, the view model will get populated
// with database search results
public ActionResult ListMyData(MyViewModel viewModel)
{
if (!string.IsNullOrEmpty(viewModel.MyField))
{
// Search database and return results
/* viewModel.SearchResults = [data from database] */
}
else
{
viewModel.MyField = "something";
}
return View("ListMyData", viewModel);
}
}
页面上我的文本框里显示了值"something",但是文本框高亮显示为红色。肯定是 [Required]
属性在起作用,因为如果我删除 [Required]
属性,红色就会消失。
为什么文本框中有数据但验证失败?
编辑:这是我的布局页面,显示了我的脚本。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@ViewBag.Title - My ASP.NET Application</title>
@Styles.Render("~/Content/css")
@Styles.Render("~/Content/DataTables/css/jquery.dataTables.css")
@Scripts.Render("~/bundles/modernizr")
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body>
@RenderBody()
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@Scripts.Render("~/Scripts/jquery.unobtrusive-ajax.min.js")
@Scripts.Render("~/Scripts/DataTables/jquery.dataTables.min.js")
@RenderSection("scripts", required: false)
</body>
</html>
问题是您从作为参数接收的 viewModel
中获取 ModelState
的验证(默认情况下没有值)。
解决这个问题的一个方法是在返回之前清除模型状态。
ModelState.Clear();
return View("ListMyData", viewModel);
我有一个搜索页面,它的模型是 MyViewModel
class。由于 [Required]
属性中缺少数据,即使该字段确实具有值,我也遇到了一个字段突出显示为红色的问题。
这是视图模型
public class MyViewModel
{
[DisplayName("My Field")]
[Required]
public string MyField { get; set; }
// This gets populated with search results from the database
// whenever the user clicks the Search button and the page
// posts back
public List<Customer> SearchResults { get; set; }
}
这是 ASP 页面
@model MyProgram.MyViewModel
@using (Html.BeginForm("ListMyData", "Test", FormMethod.Get, htmlAttributes: new { @id = "search-form", @class = "form-horizontal" }))
{
<div class="form-group">
@Html.LabelFor(model => model.MyField, new { @class = "control-label col-sm-2" })
<div class="col-sm-2">
@Html.TextBoxFor(model => model.MyField, htmlAttributes: new { @class = "form-control" })
</div>
</div>
}
这是我的控制器
public class TestController
{
// The first time that the user navigates to this page, the "else" clause
// will execute and the form will get populated with default values. When
// the user clicks the "search" button, the view model will get populated
// with database search results
public ActionResult ListMyData(MyViewModel viewModel)
{
if (!string.IsNullOrEmpty(viewModel.MyField))
{
// Search database and return results
/* viewModel.SearchResults = [data from database] */
}
else
{
viewModel.MyField = "something";
}
return View("ListMyData", viewModel);
}
}
页面上我的文本框里显示了值"something",但是文本框高亮显示为红色。肯定是 [Required]
属性在起作用,因为如果我删除 [Required]
属性,红色就会消失。
为什么文本框中有数据但验证失败?
编辑:这是我的布局页面,显示了我的脚本。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@ViewBag.Title - My ASP.NET Application</title>
@Styles.Render("~/Content/css")
@Styles.Render("~/Content/DataTables/css/jquery.dataTables.css")
@Scripts.Render("~/bundles/modernizr")
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body>
@RenderBody()
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@Scripts.Render("~/Scripts/jquery.unobtrusive-ajax.min.js")
@Scripts.Render("~/Scripts/DataTables/jquery.dataTables.min.js")
@RenderSection("scripts", required: false)
</body>
</html>
问题是您从作为参数接收的 viewModel
中获取 ModelState
的验证(默认情况下没有值)。
解决这个问题的一个方法是在返回之前清除模型状态。
ModelState.Clear();
return View("ListMyData", viewModel);