5
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; }
}
On 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 carries all topics, etc.
But I can’t make a bind in it by saving it. 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 Topic Id, even because the class is null.
So I capture using Request.Params["Topico"];
How do I link my dropdownlist to my property public virtual Topico
that I have in my Subtopic class
Very good @user6026 solved my error!
– Furlan