我正在尝试使用具有一对多关系的实体框架创建ASP.NET MVC应用程序。为此我已成功设法将相应的项目列表加载到下拉控件(在“创建”视图中),但是当我单击“创建”按钮(在“创建”视图中)页面验证为faild时,验证错误消息为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是一个有效值。我在这里缺少什么?
提前致谢...
答案 0 :(得分:2)
您没有显示Author
对象的外观,
假设它是这样的,
public class Author
{
public int Id{get;set;}
public string Name{get;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>
答案 1 :(得分:1)
你应该有这样的东西:
<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
答案 2 :(得分:1)
通过
管理解决此问题将模型更改为
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 :(得分:0)
您必须向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");