-2
I need to place a dropdownlist under the following conditions: It will display the name of the City(City Table), but will only record the Employee Code. Using this approach, I can assemble the Dropdownlist: Model
public class Cidade
{
[Key]
public int id { get; set; }
[Required(ErrorMessage = "O nome da cidade é obrigatório", AllowEmptyStrings = false)]
[Display(Name="Nome")]
public String nome { get; set; }
public List<Cidade> ListaCidades()
{
return new List<Cidade>
{
new Cidade { id = 1, nome = "SÃO PAULO"}
};
}
}
See that my model is done manually. I could not bring the data directly from the table. In the controller I did so(Before I was using in Wrong Action):
public async Task<ActionResult> Index()
{
GetFuncionariosAsync funcionarios = new GetFuncionariosAsync();
//popular a dropdown de cidades na view funcionarios
GetCidadesAsync cidade = new GetCidadesAsync();
var _cidade = await cidade.GetCidades();
ViewBag.ViewCidade = new SelectList(_cidade, "id", "nome");
var model = await funcionarios.GetFuncionarios();
return View(model);
}
// GET: GetFuncionarios/Create
public ActionResult Create(Cidade cidade)
{
var cidadeId = cidade.id;
ViewBag.ViewCidade = new SelectList
(
new Cidade().ListaCidades(),
"id",
"nome",
cidadeId
);
return View();
}
and of course my cshtml (only the dropdownlist) was like this:
<div class="form-group">
@Html.LabelFor(model => model.cidade, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("id", (SelectList)ViewBag.ViewCidade, "- Selecione -", new { @class = "form-control" })
</div>
</div>
See the screenshot below of how the dropdown looks, now I just need to know how I load the list of the database and if I really need to create the Listings() method in the model?
See that in the dropdown, I show the City of São Paulo. But this was launched manually, now how I list the fields of the comic? Do I need the listicities() method? I find it unnecessary.
The way it is, if I try to do it that way,
public async Task<ActionResult> Create()
{
//var cidadeId = cidade.id;
GetCidadesAsync cidade = new GetCidadesAsync();
var _cidade = await cidade.GetCidades();
ViewBag.ViewCidade = _cidade.Select(s => new SelectListItem { Value = s.id.ToString(), Text = s.nome });
return View();
}
I take the error below:
System.Invalidcastexception: 'Unable to cast Object of type 'Whereselectlistiterator`2[Trainingcrud.Models.City,System.Web.Mvc.Selectlistitem]' to type 'System.Web.Mvc.Selectlist'.'
Any help is welcome.
You must have a view model that includes City and Employees or can popular the Cities of
DropDownList
in hisController
and move on to theView
in aViewBag
, remembering to already pass the selected item in the case of the edit screen.– Leandro Angelo
If you create a
SelectList
inController
and pass theViewBag
will make your job a lot easier. Do a search with these tips and if you can’t implement let me know that I put a response with the code.– Leandro Angelo
No, it will not, follow the instructions I left in the comment. Ps. when I spoke at
Controller
I was referring to the Web project, not the API and the GET method– Leandro Angelo
The problem is not only in View, it comes from the structure, for today follow the steps I gave you and if you can not solve or understand the problem let me know, tomorrow will be solved. Ps.: Post the GET action of your web project, for now forget the post and your API
– Leandro Angelo
Did you do this in the POST or GET action of create? "Avoid Edit 2" update the displayed code as you change it.
– Leandro Angelo