ASP.NET MVC 页面验证失败(值无效。)
Posted
技术标签:
【中文标题】ASP.NET MVC 页面验证失败(值无效。)【英文标题】:ASP.NET MVC Page Validation Fails (The value is invalid.) 【发布时间】:2012-06-14 09:41:45 【问题描述】:我正在尝试使用具有一对多关系的实体框架创建 ASP.NET MVC 应用程序。为此,我已成功地将适当的项目列表加载到下拉控件(在创建视图中),但是当我单击创建按钮(在创建视图中)页面验证失败时,验证错误消息为 The value '1' is invalid.
。
错误
型号
public class Post
public int Id get; set;
...
public virtual Person Author get; set;
DataBaseContext
protected override void OnModelCreating(DbModelBuilder modelBuilder)
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Entity<Post>()
.HasOptional(p => p.Author);
控制器
public ActionResult Create()
PopulateAuthorDropDownList();
return View();
[HttpPost]
public ActionResult Create(Post post)
if (ModelState.IsValid)
db.Posts.Add(post);
db.SaveChanges();
return RedirectToAction("Index");
PopulateAuthorDropDownList(post.Author);
return View(post);
private void PopulateAuthorDropDownList(object selectedPerson = null)
var personQuery = from d in db.People
orderby d.Name
select d;
ViewBag.Author = new SelectList(personQuery, "Id", "Name", selectedPerson);
查看
<div class="editor-label">
@html.LabelFor(model => model.Author)
</div>
<div class="editor-field">
@Html.DropDownList("Author", String.Empty)
@Html.ValidationMessageFor(model => model.Author)
</div>
当我检查数据库中的作者表时,有 ID 为 1 的记录,所以我猜 1 是一个有效值。我在这里错过了什么?
提前谢谢...
【问题讨论】:
你从哪里得到这个错误?是例外吗?它是无效的模型状态吗?你的模型类上有 DataAnnotation 属性吗? @Jan Page.IsValid 失败并且消息显示在视图中,ValidationMessageFor
字段。
【参考方案1】:
你没有展示Author
对象的样子,
假设如果是这样,
public class Author
public int Idget;set;
public string Nameget;set;
试试这个,
<div class="editor-label">
@Html.LabelFor(model => model.Author)
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.Author.Id, ViewBag.Author, "Select an Author")
@Html.ValidationMessageFor(model => model.Author)
</div>
【讨论】:
【参考方案2】:设法解决了这个问题
将模型更改为
public class Post
public int Id get; set;
...
public int Author get; set; // changed type to int
将视图更改为
<div class="editor-label">
@Html.LabelFor(model => model.Author)
</div>
<div class="editor-field">
@Html.DropDownList("AuthorId", String.Empty)
@Html.ValidationMessageFor(model => model.Author)
</div>
将控制器改为
private void PopulateAuthorDropDownList(object selectedPerson = null)
var personQuery = from d in db.People
orderby d.Name
select d;
ViewBag.AuthorId = new SelectList(personQuery, "Id", "UserName", selectedPerson); //Changed Author to AuthorId
并删除
modelBuilder.Entity<Post>()
.HasOptional(p => p.Author);
来自 DataBaseContext
无论如何感谢您的回答... :)
【讨论】:
【参考方案3】:你应该有这样的东西:
<div class="editor-label">
@Html.LabelFor(model => model.Author.Name)
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.Author.Name, (SelectList)ViewBag.AuthorList)
@Html.ValidationMessageFor(model => model.Author.Name)
</div>
请注意,您应该命名 ViewBag.AuthorList 而不是 ViewBag.Author
【讨论】:
感谢您的回答,但显示相同的错误消息 尝试从下拉列表中删除 String.Empty【参考方案4】:您必须向DropDownList
提供一个选项列表,其中您只是传递string.empty
。
你应该使用强类型版本DropDownListFor
:
@Html.DropDownListFor(model => model.Author.Name, Model.AuthorList)
或者不使用名称而是使用 ID 可能会更好:
@Html.DropDownListFor(model => model.Author.Id, Model.AuthorList)
还有你的模型:
class Model
...
public SelectList AuthorList get;set;
在你的控制器中:
model.AuthorList = new SelectList(fetchAuthors()
.Select(a => new SelectListItem
Text = a.Name,
Value = a.Id
, "Value", "Text");
【讨论】:
以上是关于ASP.NET MVC 页面验证失败(值无效。)的主要内容,如果未能解决你的问题,请参考以下文章
ASP.NET Core 2.1:在 HTTP POST 验证失败后导航回页面显示浏览器错误
Asp.net MVC 设置验证日期格式在 Chrome 上失败