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

3

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

  

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

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

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 have tried to put the property "UserId" to "Id" pens, because Identity uses only "Id", even though it did not work.

    
asked by anonymous 28.12.2017 / 15:34

2 answers

0

You have missed the fourth and last parameter to indicate which of the items is selected :

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

var id = 1; // aqui você coloca qual dos itens é para ficar selecionado

// e aqui você passar o 4 parâmetro para que os item fique selecionado
ViewBag.UsuarioId = new SelectList(items, "UsuarioId", "Nome", id);

This is the example given where the first SelectList is selected and the second is not.

//with selected value specified
var selectItems = new SelectList(brands, "BrandId", "BikeBrand", 2);

//only the items list
var selectItems = new SelectList(brands, "BrandId", "BikeBrand");

28.12.2017 / 15:46
1

Another way is to use DropDownListFor.

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

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

    
28.12.2017 / 17:10