Come on...
As there is very little information in the question, I will assume some things, in order to set an example that may be useful to you.
I’ll give you a hint of how you can create your DropDownList
there are other ways to create, but I’ll suggest it the way I do it.
I will assume that a Genero
has several works, then his models
will stay like this:
[Table("Generos")]
public class Genero
{
[Key]
public int GeneroId { get; set; }
public string NomeGenero { get; set; }
public string Descricao { get; set; }
public virtual ICollection<Obra> Obras { get; set; }
}
[Table("Obras")]
public class Obra
{
[Key]
public int ObraId { get; set; }
public int GeneroId { get; set; }
public string NomeObra { get; set; }
public string Autor { get; set; }
public string Editora { get; set; }
public string DescricaoObra { get; set; }
public virtual Genero Genero { get; set; }
}
So, your Create
Obrascontroller
will look like this:
public ActionResult Create()
{
//Aqui preenche-se uma ViewBag para usar na View
ViewBag.Generos = db.Generos.ToList();
return View();
}
And its View
de Create will look like this:
<div class="col-md-3">
<label class="control-label">Status</label>
@Html.DropDownListFor(model => model.ObraId, ((IEnumerable<Genero>)ViewBag.Generos).Select(option => new SelectListItem
{
Text = option.NomeGenero,
Value = option.GeneroId.ToString(),
Selected = (Model != null) && (Model.ObraId == option.GeneroId)
}), "Selecione um Gênero...", new { @class = "form-control", @placeholder = "Genero" })
</div>
Doing so, in creating a work you will be able to choose which genre it belongs to.
Thank you very much solved, only instead of Model.Obraid == option.Generoid I put backwards option.Generoid == Model.Obraid Dai worked
– Pedro Henrique
@Pedrohenrique, if the answer was helpful mark it as such.
– Renan Carlos
I don’t know how you do it :v
– Pedro Henrique
@Pedrohenrique When a reply meets what you need, you can mark as a response, as you have already done and also vote as a positive, "cetinha" up next to the answer. You can do that to the comments too.
– Renan Carlos