Monday, August 20, 2012

Dropdownlist Binding in MVC without Database

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>




No comments:

Using Authorization with Swagger in ASP.NET Core

 Create Solution like below LoginModel.cs using System.ComponentModel.DataAnnotations; namespace UsingAuthorizationWithSwagger.Models {     ...