2

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

错误

模型

public class Post
{
    public int Id { get; set; }
    ...
    public virtual Person Author { get; set; }
}

数据库上下文

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 是一个有效值。我在这里想念什么?

提前致谢...

4

4 回答 4

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>
于 2012-06-14T10:05:05.337 回答
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

于 2012-06-14T09:51:27.937 回答
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);

数据库上下文

无论如何感谢您的答案... :)

于 2012-06-15T04:00:37.193 回答
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");
于 2012-06-14T10:08:16.037 回答