ASP.NET MVC 5, I can't get set when I go into edit

Question:

I have a service class and in it I have the User responsible for service, which is a user of IdentityUser .

Remembering that I changed the Identity Id from string to "Int", just that.

I can register normally, however, when I go to edit, it lists the users, but does not bring the user who is already registered in the service set.

Entity:

[Table("Atendimentos")]
public class Atendimento
{
    [Key]
    public int AtendimentoId { get; set; }
    public int UsuarioId { get; set; }

    [Display(Name = "Responsável pelo atendimento")]
    public virtual Usuario Usuario { get; set; }
}

Controller:

ViewBag.UsuarioId = new SelectList(db.Users.Select(x => new { UsuarioId = x.Id, Nome = x.Nome + " " + x.Sobrenome }), "UsuarioId", "Nome");

View:

@Html.DropDownList("UsuarioId", null, htmlAttributes: new { @class = "form-control" })

I've already tried to set the property "UsuarioId" to "Id" only, because Identity only uses "Id", it still didn't work.

Answer:

Another way is to use DropDownListFor.

@Html.DropDownListFor(model => model.UsuarioId, new SelectList(ViewBag.Usuarios, "UsuarioId", "Nome"))

You say which property of your model should be used to set the value and which list should fill the options.

Scroll to Top