2

I'm trying to create ASP.NET MVC Application with Entity Framework, which has One to Many relationship. For that I have successfully managed to load the appropriate list of items to a dropdown control (in Create view), But when I click the Create button (in Create view) page validation is faild, validation error message is The value '1' is invalid..

Error

Model

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);
}

Controller

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);
    }

View

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

When I check Author table in database there is Record with Id 1, so I guess 1 is a valid value. What am I missing here?

Thanks in advance...

2
  • Where do you get that error? Is it an exception? Is it invalid ModelState? Do you have DataAnnotation attributes on your model class? Commented Jun 14, 2012 at 9:46
  • @Jan Page.IsValid fails and message is displayed in the view, ValidationMessageFor field. Commented Jun 14, 2012 at 9:55

4 Answers 4

2

You didn't show how the Author object looks like,

Suppose if it is like this,

public class Author
{ 
   public int Id{get;set;}
   public string Name{get;set;}
}

Try this,

<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>
Sign up to request clarification or add additional context in comments.

Comments

1

Managed to fix this by

changing Model to

public class Post
{
    public int Id { get; set; }
    ...
    public int Author { get; set; } // changed type to int
}

changing View to

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

changing Controller to

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
    }

and removing

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

from DataBaseContext

Anyway Thanks for the answers... :)

Comments

1

You should have something like this:

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

Note that you should name ViewBag.AuthorList instead of ViewBag.Author

2 Comments

Thanks for the answer, but with this same error message is displayed
Try removing String.Empty from dropdownlist
0

You have to provide a list of options to DropDownList where you are just passing string.empty.

And i You should use the strongly typed version DropDownListFor:

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

Or maybe even better by not using the name but the Id:

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

And your model:

class Model {
    ...
    public SelectList AuthorList {get;set;}
}

And in your controller:

model.AuthorList = new SelectList(fetchAuthors()
                         .Select(a => new SelectListItem {
                                          Text = a.Name,
                                          Value = a.Id 
                                      }, "Value", "Text");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.