0
I’m having a problem with Dropdownlistfor, it’s populated with database data.
Controller Code.
var coordenacaoList = db.VW004_LISTAR_COORDENACAO.ToList();
ViewBag.CoordenacaoList = new SelectList(coordenacaoList, "ID_COORDENACAO", "TITULO_COORDENACAO");
View.
@Html.DropDownListFor(model => model.Usuario.T003_FK_COORDENACAO, (SelectList)ViewBag.CoordenacaoList, " Selecione uma Coordenação", new { @class = "form-control" })
Only when I’m going to post on a trial, give me that mistake.
Controller.
[HttpPost]
public ActionResult Cadastrar(T001_USUARIO obj)
{
try
{
SqlParameter param1 = new SqlParameter("@MATRICULA_CADASTRADA", obj.T001_MATRICULA);
SqlParameter param2 = new SqlParameter("@NOME", obj.T001_NOME_USUARIO);
SqlParameter param3 = new SqlParameter("@PERFIL", obj.T002_FK_PERFIL);
SqlParameter param4 = new SqlParameter("@COORD", obj.T003_FK_COORDENACAO);
var db = new AppModel();
var data = db.Database.ExecuteSqlCommand("SP001_CADASTRA_USUARIO @MATRICULA_CADASTRADA, @NOME, @PERFIL, @COORD", param1, param2, param3, param4);
return RedirectToAction("Listar");
}
catch
{
return View();
}
}
Controller GET.
public ActionResult Cadastrar()
{
AppModel db = new AppModel();
var coordenacaoList = db.T003_COORDENACAO.ToList();
ViewBag.CoordenacaoList = new SelectList(coordenacaoList, "T003_ID_COORDENACAO", "T003_DESC_COORD");
var perfilList = db.VW005_LISTAR_PERFIL.ToList();
ViewBag.PerfilList = new SelectList(perfilList, "T002_ID_PERFIL", "T002_DESC_PERFIL");
return View();
}
Lucas, please enter the code of your model.
– Cassio Alves
public partial class T001_USUARIO { public int T001_ID_USUARIO { get; set; } public string T001_MATRICULA { get; set; } public string T001_NOME_USUARIO { get; set; } public Nullable<bool> T001_ATIVO { get; set; } public Nullable<int> T002_FK_PERFIL { get; set; } public Nullable<int> T003_FK_COORDENACAO { get; set; } }
– lucas francisco