在MVC 5中将下拉列表添加到Register视图

时间:2015-04-14 14:40:17

标签: asp.net-mvc-5 user-identification

我想在MVC 5帐户平台的默认Register视图中添加一个下拉列表。但是我不能用View-Models做到这一点,也无法用适当的数据填充它。有人有什么想法吗?

2 个答案:

答案 0 :(得分:1)

public string  AddEmployees  (RegisterViewModel   model)

   {
        //create user 
        var   user   = new   ApplicationUser()
        {
            UserName   =   model.Email,
            Email   =   model.Email,
            UserProfile   = new Employees
            {
                UserProfileId   =  emphasized text model.UserProfileId,
                FirstName = model.FirstName,
                LastName = model.LastName,
                Gender = gender,
                ContactNumber = model.ContactNumber,
                Email = model.Email,
                PhysicalAddress = model.PhysicalAddress,
                IdentityNumber = model.IdentityNumber,
                IsFirstTimeLogin = true,
                Position = model.Position
            }
        };

        //create user in the aspnet table
        var result = UserManager.Create(user, password);


        if (result.Succeeded)
        {
            "Display your Message";

            //create role
            if (!RoleManager.RoleExists(model.Position))
            {
                 //create a role
            }
            //assign user to role
            UserManager.AddToRole(user.Id, model.Position);
        }
        return feedback;
    }

答案 1 :(得分:0)

@model Ingogo.Model.Employee_Management.Model_View.RegisterViewModel

@{
    ViewBag.Title = "Register";
    Layout = "~/Views/Shared/_AdminClerk.cshtml";
}

<h2><span><i class="glyphicon glyphicon-briefcase"></i></span>&nbsp;Add Employee</h2>

@if (ViewBag.Error == "There Is Already An Employee With The Same Information")
{
    <p class="alert alert-danger">@ViewBag.Error</p>
}
@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">

        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-4">
                @Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control", placeholder = "Enter Employee First Name", Title = "Example: Prince" } })
                @Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-4">
                @Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control", placeholder = "Enter Employee Surname", Title = "Example: Buthelezi" } })
                @Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
            </div>
        </div>


        <div class="form-group">
            @Html.LabelFor(model => model.IdentityNumber, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-4">
                @Html.EditorFor(model => model.IdentityNumber, new { htmlAttributes = new { @class = "form-control", onkeypress = "return isNumberKey(event)", maxlength = "13", @Value = "", placeholder = "Enter South African Identity Number", Title = "Must Be 13 Digits" } })
                <script type="text/javascript">
                    function isNumberKey(evt) {
                        var charCode = (evt.which) ? evt.which : event.keyCode;
                        if (charCode != 46 && charCode > 31
                          && (charCode < 48 || charCode > 57))
                            return false;

                        return true;
                    }

                </script>

                @Html.ValidationMessageFor(model => model.IdentityNumber, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-4">
                @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control", placeholder = "Enter Employee Working Email Address", Title = "Example: example@somewhere.com" } })
                @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
            </div>
        </div>

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


        <div class="form-group">
            @Html.LabelFor(model => model.ContactNumber, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-4">
                @Html.EditorFor(model => model.ContactNumber, new { htmlAttributes = new { @class = "form-control", onkeypress = "return isNumberKey(event)", maxlength = "10", @Value = "0", placeholder = "Enter Employee Cell Number", Title = "Example: 0782115579" } })
                <script type="text/javascript">
                    function isNumberKey(evt) {
                        var charCode = (evt.which) ? evt.which : event.keyCode;
                        if (charCode != 46 && charCode > 31
                          && (charCode < 48 || charCode > 57))
                            return false;

                        return true;
                    }

                </script>
                @Html.ValidationMessageFor(model => model.ContactNumber, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.Position, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-4">
                @Html.DropDownListFor(g => g.Position, new SelectListItem[]
                {
                    new SelectListItem {Value = "Farm Manager", Text = "Farm Manager"},
                    new SelectListItem {Value = "Supervisor", Text = "Supervisor"},
                    new SelectListItem {Value = "Admin Clerk", Text = "Admin Clerk"},
                    new SelectListItem {Value = "Farm Assistance", Text = "Farm Manager Assistance"}
                }, "--Select Employee type--", new { @class = "form-control", title = "The employee could be of the listed job titles" })
                @Html.ValidationMessageFor(model => model.Position, "", new { @class = "text-danger" })
            </div>
        </div>

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



 <div>
        @Html.ActionLink("Back to List", "GetAllEmployees")
    </div>