I have my class:
public class Topico
{
public int Id { get; set; }
public string Nome { get; set; }
public string Observacao { get; set; }
}
and my SubTopic class
public class SubTopico
{
public int Id { get; set; }
public virtual Topico Topico { get; set; }
public int? TopicoId { get; set; }
public string Nome { get; set; }
public string Observacao { get; set; }
}
In my controller I have:
public ActionResult Create()
{
ViewBag.Topico = new SelectList(db.Topicoes, "Id", "Nome");
return View();
}
And in my view I have my DropDownList
@Html.DropDownList("Topico",string.Empty)
Until then it loads all topics, etc.
But I can not do a Bind on it when saving. In my action to Save
public ActionResult Create([Bind(Include="Id,Topico,Nome,Observacao")] SubTopico subtopico)
{
if (ModelState.IsValid)
{
subtopico.TopicoId = Convert.ToInt32(Request.Params["Topico"]);
db.SubTopicoes.Add(subtopico);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(subtopico);
}
It does not come from the Topic Id, because the class is null.
So I capture using Request.Params["Topico"];
How do I link my dropdownlist with my public virtual Topico
property that I have in my SubTopic class