ASP.NET MVC 5, I can’t bring set when I edit

Asked

Viewed 83 times

3

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

Remembering that I changed the Identity id from string to "Int", that’s all.

I can register normally, however, when I edit, it lists users, but does not bring 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 "User" property for "Id" feathers, because Identity only uses "Id", even though it didn’t work.

2 answers

1

Another way is to use Dropdownlistfor.

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

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

0


Failed to pass fourth and last parameter to indicate which of the items is to stay 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 not.

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

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

References

  • 2

    It worked perfectly! Thank you very much for the reply.

Browser other questions tagged

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