2012-06-14 33 views
2

我试图创建具有一对多关系的实体框架的ASP.NET MVC应用程序。为此,我已成功设法将适当的项目列表加载到下拉控件(在“创建”视图中),但是当我单击“创建”按钮(在“创建”视图中)页面验证为faild时,验证错误消息为The value '1' is invalid.ASP.NET MVC页面验证失败(值无效)

错误

模型

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

你在哪里得到这个错误?这是一个例外吗?它是无效的ModelState?你的模型类是否有DataAnnotation属性? – Jan

+0

@Jan Page.IsValid失败,消息显示在视图“ValidationMessageFor”字段中。 – Nalaka526

回答

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> 

改变控制器

01来解决这个问题
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 
    } 

DataBaseContext

总之谢谢你的答案删除

modelBuilder.Entity<Post>() 
       .HasOptional(p => p.Author); 

... :)

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

+0

感谢您的回答,但显示相同的错误消息 – Nalaka526

+0

尝试从下拉列表中删除String.Empty – karaxuna

+0

并使用dropdownlistfor。请参阅更新 – karaxuna

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> 
0

你必须提供的选项列表DropDownList,你只是过客string.empty

和我你应该使用强类型版本DropDownListFor

@Html.DropDownListFor(model => model.Author.Name, Model.AuthorList) 

,或者甚至更好的通过不使用名称,但编号:

@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");