dropdow list MVC, with selected item

Asked

Viewed 26 times

1

I have a dropdowlist that carries RamosDeAtidade, want to know how I do to my Edit view, already be selected the branch I registered, in my Controller, at Action Edit It’s like this:

var cliente = _clienteAppService.ObterPorId(id ?? Guid.Empty);
ViewBag.RamoAtividadeId = new SelectList(_ramoDeAtividadeAppService.ObterTodos(), 
         "RamoDeAtividadeId", 
         "Descricao", 
          cliente.RamoAtividadeId);
return PartialView(cliente);

in my view, I did the following Razor:

@Html.DropDownListFor(model => model.RamoAtividadeId, (IEnumerable<SelectListItem>)ViewBag.RamoAtividadeId,  new { @class = "form-control-sm", style = "max-width:156px; padding:0" })

in this way, the branch that I registered is never selected.

  • you have done this by passing the third parameter in the class SelectList!

  • but still, the branch of activity that I registered is not selected. :/

1 answer

1


Actually you are already doing this because you are passing the third parameter just make an adjustment in the View and use DropDownList, example:

@Html.DropDownList("RamoAtividadeId", null, null, new { @class = "form-control-sm", style = "max-width:156px; padding:0" })

or can use and the cast for SelectList, example:

@Html.DropDownListFor(model => model.RamoAtividadeId, ViewBag.RamoAtividadeId as SelectList,  new { @class = "form-control-sm", style = "max-width:156px; padding:0" })
  • now it worked. Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.