locked
Select Tag not binding with model property. RRS feed

  • Question

  • User159480641 posted

    EDIT: I solved it, i didn't realize in my view there is a jquerry method that change the value, when I comment it works well. Sorry and thanks for reading.

    Hi, I can´t set the property value to a select, in my model I have

    public class UsuarioSistema : IValidatableObject
        {
            ...
            public int Legajo { get; set; }
            public SelectList Legajos { get; set; }
        }

    my edit view

     @if (Model.Legajo > 0)
    {
       <label class="control-label">: @Model.LegajoNombre</label>
       <select asp-for="Legajo" asp-items="Model.Legajos" class="form-control">
          <option value="">Seleccionar...</option>
       </select>
       <span asp-validation-for="Legajo" class="text-danger"></span>
    }

    and in my controller

    public async Task<IActionResult> Edit(int? id)
            {
                ...
    //UsuarioSistema.Legajo = 2;
    UsuarioSistema.Legajos = new SelectList(await _context.Empleados.ToListAsync(), "EmpleadoId", "LegajoNombre", UsuarioSistema.Legajo); return View(UsuarioSistema); }

    When load the edit view, the select show me all the options as I expected, but not in the selected value I set, it always show me the "default" option <option value="">Seleccionar...</option>

    any suggestion?

    Thanks!

    EDIT: I solved it, i didn't realize in my view there is a jquerry method that change the value, when I comment it works well. Sorry and thanks for reading.

    Monday, August 12, 2019 10:36 PM

Answers

  • User159480641 posted

    I solved it, i didn't realize in my view there is a jquerry method that change the value, when I comment it works well. Sorry and thanks for reading.

    $(document).ready(function(){
        $("#Legajo").val("").change();
    });

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 13, 2019 12:50 PM

All replies

  • User1520731567 posted

    Hi guillos,

    I'm glad that you have solved the issue by yourself.

    I suggest you could post the details answer as reply and mark it as answer.

    This will help other people who faces the same issue. Thank you very much.

    Best Regards.

    Yuki Tao

    Tuesday, August 13, 2019 1:57 AM
  • User159480641 posted

    I solved it, i didn't realize in my view there is a jquerry method that change the value, when I comment it works well. Sorry and thanks for reading.

    $(document).ready(function(){
        $("#Legajo").val("").change();
    });

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 13, 2019 12:50 PM