DropDownList остается пустым - PullRequest
0 голосов
/ 07 июня 2018

Я много искал и не могу точно определить, где проблема, он возвращает пустой DropDownList с текстом «Seleccione Rol», так что я думаю, проблема в том, что DDL не заполняется, яочень ценю любую помощь.

Контроллер:

public ActionResult Usuario_rol()
{
    using (proyectob_dbEntities db = new proyectob_dbEntities())
    {
        usuario_rol model = new usuario_rol();
        model.rolesList = db.roles;

        //roles model2 = new roles();
        //model.roles.rolesl = db.roles;
    }
    return View(new usuario_rol());           
}

Вид:

<div class="form-group">
    @Html.LabelFor(model => model.roles, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-5">
        @Html.DropDownListFor(model => model.id_rol, new SelectList(Model.rolesList, "id", "rol"),"Seleccione Rol")
    </div>
</div>

Модель:

public partial class usuario_rol
{
    public string id { get; set; }
    public string id_usuario { get; set; }
    public string id_rol { get; set; }
    public System.DateTime fecha_inicio_rel { get; set; }
    public System.DateTime fecha_termino_rel { get; set; }

    public virtual contactos contactos { get; set; }
    public virtual roles roles { get; set; }

    public virtual IEnumerable<roles> rolesList { get; set; }

    public usuario_rol()
    {
        rolesList = new List<roles>();
    }

БД:

the DB looks like this

Ответы [ 2 ]

0 голосов
/ 07 июня 2018

я думаю, что это должно работать:

Контекст:

public class RoleContext  
{  
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MYConnector"].ToString());  
    public IEnumerable<RoleTable> GetRoleList()  
    {  
        // it would be good if you put inline query in procedure
        string query = "SELECT RoleName,RoleId FROM RoleTable";  
        var result = con.Query<RoleTable>(query);  
        return result;  
   }  
}

ActionMethod:

public ActionResult Usuario_rol()
{
    RoleContext roleList = new RoleContext();
    usuario_rol model = new usuario_rol();
    using (proyectob_dbEntities db = new proyectob_dbEntities())
    {
        model.rolesList = new SelectList(roleList.GetRoleList(),"RoleName","RoleId");
    }
    return View(model);
}

И представление:

@namespace of the model
<div class="form-group">
@Html.LabelFor(model => model.roles, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-5">
    @Html.DropDownListFor(model => model.id_rol, model.rolesList ,"Seleccione Rol")
</div>

0 голосов
/ 07 июня 2018

Вы не отправляете модель для просмотра, вы передаете новый объект, который пуст для просмотра.

Измените return View(new usuario_rol()); с этим return View(model);

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...