1
I need to set the default value displayed by a @Html.DropDownListFor
Researching found: @Html.Dropdownlistfor how to set default value
So I did it in my code:
@Html.DropDownListFor(model => model.equipe, new SelectList(ViewBag.equiColaborador,"id","nome","Selecionar.."), htmlAttributes: new { @class = "form-control"})
But, unsuccessfully, the first record is always presented.
Man Controller
:
// GET: Colaboradores
public ActionResult Index()
{
if (Session["cod_cli"] != null)
{
string cod_cli = Session["cod_cli"].ToString();
ViewBag.equiColaborador = db.Equipes.ToList();
return View();
}
else
{
return RedirectToAction("Login", "Account");
}
}
Friend, but between the
DropDownList
and theDropDownListFor
, I see people talking aboutDropDownListFor
is more recommended.. for being strongly typed and etc.. what you think?– Thomas Erich Pimentel
I prefer not to use Dropdownlist because it has a weak type, however, in certain cases it serves me more compared to the more typed code... It will depend on your need, but the end result for the user will be the same. Here is a good explanation of the comparison between the two, https://stackoverflow.com/questions/8182280/difference-between-dropdownlist-dropdownlistfor-html-helper
– Thiago Araújo
Friend, perfect. , it worked.. but this way, I’m forcing the user to select an item.. would have to not have this obligation?
– Thomas Erich Pimentel
Where it is -- Select -- you can switch to -- None -- and in the controller, it will receive 0 or null, depending on your field, then just do the treatment there, if you need.
– Thiago Araújo