Edit MVC with Foreign Key

Asked

Viewed 126 times

3

I need to upgrade my editLivros, Next I’ll explain my project.

Autores
-------
IdAutor
Nome

Livros
--------
IdLivro
Nome
IdAutor

I am registering the Authors and Books correctly, when I am registering the Livro, I make a ViewBag for the list of authors :

LivrosController to catch the Authors:

ViewBag.IdAutor = new SelectList(db.Autores, "IdAutor", "Nome");

DropDown of the Authors in View Create Books:

@Html.DropDownList("IdAutor", ViewBag.IdAutor as SelectList, new { @class = "form-control" })

Model Books

    public partial class Livros
{
    [Key]
    public int IdLivro { get; set; }

    [Required]
    [StringLength(50)]
    public string Nome { get; set; }

    public int IdAutor { get; set; }

    public virtual Autores Autores { get; set; }

}

Model authors

    public partial class Autores
{
    [Key]
    public int IdAutor { get; set; }

    [StringLength(50)]
    public string Nome { get; set; }
}

Problem:

My problem, is when I edit the book, automatically the Visual Studio picks up the Nome of the book and the IdAutor of the book, however, needed to generate a dropbox with the authors, yes edit the author of the book, passing the model Idautor Autores

Thank you.

  • That is, do you want to be able to save several authors for a book? Post how are the Models Authors and Books.

  • @Randrade actually friend, I wanted to leave a dropdown with the authors in the Edit view, then I choose the author by name and he picks up the Idautor, thank you for answering

  • The same thing of this answer?

  • @Randrade put in the question the Model of Authors and Books

  • actually no, because he doesn’t get Idautor @Randrade

  • Not only submit a Selectlist again in Viewbag and create the Dropdownlist?

  • Orra, I got @Jedaiasrodrigues , was under the nose kkk, tell me one thing, and to leave the author who is default in the dropdown?

  • I’ll edit a reply!

  • Ok @Jedaiasrodrigues thanks in advance

Show 4 more comments

2 answers

5

Well, just keep doing as you are. Sending the ViewBag with the SelectList for View Create Books, but in this case you will already send a selected value.

By creating a SelectList, it is possible to set as parameter the item to be selected (details). So when you arrive at your View, your DropDownList will present all available items, but will leave marked the indicated item.

Your code would look like this:

ViewBag.IdAutor = new SelectList(db.Autores, "IdAutor", "Nome", Livro.IdAutor);

There is also the possibility of doing as in reply that @Randrade posted in the comments, what changes is only the context.

  • so it wasn’t, I did it another way and it was

  • so it works too, but like you did?

  • look at the answer I made, I think it might be easier, I know, what do you think?

  • Works and solves the problem.

1


No Edit do LivrosController:

ViewBag.listaAutores = db.Autores;

Na View do Livros:

@Html.DropDownListFor(model => model.IdAutor, ((IEnumerable<MVCCodeFirst.Models.Autores>)ViewBag.listaAutores).Select(option => new SelectListItem
{
    Text = option.Nome,
    Value = option.IdAutor.ToString(),
    Selected = (Model != null) && (Model.IdAutor == option.IdAutor)
}), "Selecione...", new { @class = "form-control" })
  • 1

    http://answall.com/questions/99307/edit-mvc-com-foreign-key#comment202000_99307... xD

Browser other questions tagged

You are not signed in. Login or sign up in order to post.