ASP.NET MVC页面验证失败(该值是无效的。)(ASP.NET MVC Page Valida

2019-09-18 03:18发布

我试图创建实体框架,它具有一对多的关系ASP.NET MVC应用程序。 对于我已经成功地管理加载项的适当列表下拉控制(在创建视图),但是当我点击创建按钮(在创建视图)页面验证zhcon失败,验证错误消息是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是一个有效的值。 我缺少的是在这里吗?

提前致谢...

Answer 1:

你没有表现出多么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>


Answer 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

总之谢谢你的答案... :)



Answer 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



Answer 4:

你必须提供的选项列表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");


文章来源: ASP.NET MVC Page Validation Fails (The value is invalid.)