Dropdownlist Binding in MVC without Database

Unknown | 10:01 AM |

Staff (Model)


       public string branchid { get; set; }
        public IEnumerable<SelectListItem> BranchOptions { get; set; }

        public string countryid { get; set; }
        public IEnumerable<SelectListItem> CountryOptions { get; set; }

     
        public IEnumerable<SelectListItem> DepartmentOptions { get; set; }

AdminController (Controller)


  public enum Departments { All, Marketing, Front, Councellor, Application, Documentation, Accounts, Franchisee, Employer,Agency,Institution,Training }
        [HttpGet]
        public ActionResult AddStaff()
        {
            var model = new Staff();
            model.DepartmentOptions = Enum.GetNames(typeof(Departments))
            .Select(c => new SelectListItem() { Text = c, Value = c })
            .ToArray();

            model.CountryOptions = GetCountries("0");
            model.BranchOptions = GetBranches ("0");

            return View(model);
        }
        private List<SelectListItem> GetCountries(string defaultValue)
        {
            List<SelectListItem> items = new List<SelectListItem>();
            items.Add(new SelectListItem { Text = "Select", Value = "0", Selected = (defaultValue == "0") });
            SqlCommand cmd = new SqlCommand("select id,cntry from cntry", cn);
            cn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                items.Add(new SelectListItem { Text = dr["cntry"].ToString(), Value = dr["id"].ToString() });
            }
            dr.Close();
            cn.Close();

            return items;
        }


  private List<SelectListItem> GetBranches(string defaultValue)
        {
            List<SelectListItem> items = new List<SelectListItem>();
            items.Add(new SelectListItem { Text = "Select", Value = "0", Selected = (defaultValue == "0") });
            SqlCommand cmd = new SqlCommand("select id,branch from Branch", cn);
            cn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                items.Add(new SelectListItem { Text = dr["branch"].ToString(), Value = dr["id"].ToString() });
            }      
            dr.Close();
            cn.Close();

            return items;
        }


AddStaff (View)


@model CabAutomationSystem.Models.Staff

@{            
    ViewBag.Title = "AddStaff";
    Layout = "~/Views/Admin/Master.cshtml";
}


<body>
<h1>Add New Staff</h1>
    @using (Html.BeginForm()) {
        @Html.ValidationSummary(true, "Insertion was unsuccessful. Please correct the errors and try again.")
        <div>
            <fieldset>
                <legend>Staff Information</legend>
             
                <div class="editor-label">
                    @Html.LabelFor(m => m.department)
                </div>
                <div class="editor-field">                
                     @Html.DropDownListFor(model => model.department, new SelectList(Model.DepartmentOptions, "Value", "Text"))
                 
                    @Html.ValidationMessageFor(m => m.department)
                </div>
             
                <div class="editor-label">
                    @Html.LabelFor(m => m.uname)
                </div>
                <div class="editor-field">
                    @Html.TextBoxFor(m => m.uname)
                    @Html.ValidationMessageFor(m => m.uname)
                </div>
             
                <div class="editor-label">
                    @Html.LabelFor(m => m.pwd)
                </div>
                <div class="editor-field">
                    @Html.PasswordFor(m => m.pwd)
                    @Html.ValidationMessageFor(m => m.pwd)
                </div>

                 <div class="editor-label">
                    @Html.LabelFor(m => m.name)
                </div>
                <div class="editor-field">
                    @Html.TextBoxFor(m => m.name)
                    @Html.ValidationMessageFor(m => m.name)
                </div>

                 <div class="editor-label">
                    @Html.LabelFor(m => m.address)
                </div>
                <div class="editor-field">
                    @Html.TextBoxFor(m => m.address)
                    @Html.ValidationMessageFor(m => m.address)
                </div>

                 <div class="editor-label">
                    @Html.LabelFor(m => m.phoneno)
                </div>
                <div class="editor-field">
                    @Html.TextBoxFor(m => m.phoneno)
                    @Html.ValidationMessageFor(m => m.phoneno)
                </div>

                 <div class="editor-label">
                    @Html.LabelFor(m => m.email)
                </div>
                <div class="editor-field">
                    @Html.TextBoxFor(m => m.email)
                    @Html.ValidationMessageFor(m => m.email)
                </div>

                  <div class="editor-label">
                    @Html.LabelFor(m => m.country)
                </div>
                <div class="editor-field">                
                     @Html.DropDownListFor(model => model.countryid, new SelectList(Model.CountryOptions, "Value", "Text"))
                 
                    @Html.ValidationMessageFor(m => m.countryid)
                </div>

                  <div class="editor-label">
                    @Html.LabelFor(m => m.branch)
                </div>
                <div class="editor-field">                
                     @Html.DropDownListFor(model => model.branchid, new SelectList(Model.BranchOptions, "Value", "Text"))
                 
                    @Html.ValidationMessageFor(m => m.branchid)
                </div>
                           
                <p>
                    <input type="submit" value="Insert" />
                </p>
            </fieldset>
        </div>
    }
</body>

Category:

About http://dotnetvisual.blogspot.in/:
DOT NET TO ASP.NET is a web application framework marketed by Microsoft that programmers can use to build dynamic web sites, web applications and web services. It is part of Microsoft's .NET platform and is the successor to Microsoft's Active Server Pages (ASP) technology. ASP.NET is built on the Common Language Runtime, allowing programmers to write ASP.NET code using any Microsoft .NET language. create an application very easily ....