Hi. I have three tables on my database:
Genre(IdGenre, Genre, Description)
Text(IdText, IdUser, Text)
TextGenre(IdTextGenre, IdText(FK), IdGenre(FK)
)
This third tables is to connect text and genre, because I want the table text to have until 2 genres, and When I search for a genre, automatically it shows the text that are in such genre.
My question is: How do I get an Id from a selected item in dropdownlist? Here is my DropDownList
@Html.DropDownListFor(model => model.IdGenero, ((IEnumerable<AllFiction.Models.Genre>
)ViewBag.Genres).Select(option => new SelectListItem
{
Text = option.Genre,
Value = option.IdGenre.ToString(),
Selected = (Model != null) && (Model.IdGenre== option.IdGenre)
}), "Select a Genre")
And here is my action to create Texts:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult CreateText(Text text)
{
if (ModelState.IsValid)
{
db.Text.Add(text);
TextGenre tg = new TextGenre();
tg.IdText = text.IdText;
tg.IdGenre =
db.SaveChanges();
return RedirectToAction("Index", "User");
}
ViewBag.Genres= db.Genre.ToList();
return View(text);
}
Anyone know how can I do this? Thanks!