如何禁用从数据库检索的下拉项

时间:2019-05-19 19:56:12

标签: asp.net-mvc

我有两个表:

  • USER_ROLE_TABLE(ID,角色名)

    id     role_name
    -----------------
      1    Admin
      2    Faculty
      3    Student
    
  • USER_TABLE(ID,名称,角色ID)

     id   name    roleid
     -------------------
      1   user1      3
      2   user2      3
      3   user3      1
    

在注册过程中,我想首先检查具有admin角色的用户是否存在。如果不是,那么我只想在下拉菜单中显示admin选项,否则它将显示所有角色,包括admin。该怎么做?

我只是asp.net的初学者。您的帮助将不胜感激。谢谢

// GET: users/Create
    public ActionResult Create()
    {
        ViewBag.role_id = new SelectList(db.user_role, "id", "role_name");
        return View();
    }

Create.cshtml

@model Test_drpdown.datamodel.user

@{
ViewBag.Title = "Create";
}
@using (Html.BeginForm()) 
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
    <h4>user</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.name, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.name, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.name, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.role_id, "role_id", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("role_id", null, htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.role_id, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>
</div>
}

0 个答案:

没有答案