Wednesday 12 October 2016

Bind Dropdown in different ways in MVC

In MVC, drop down can be bind in different ways. Such as using ViewBag, Selectlist, Directly bind on the view etc. Following are some way through which we can bind drop down in MVC.

View (BindAllDropdown.cshtml)

@model MvcAccMgmt.Models.DropdownModel
@{
    ViewBag.Title = "BindAllDropdown";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Bind Dropdown in different way</h2>

@using (Html.BeginForm("Index", "Ajax", FormMethod.Post))
{
    <div ng-app="" ng-controller="ajaxCntr" style="height:400px; width:500px;">
        <div class="row">
            <div class="col-md-6">
                <div class="form-group">
                    <strong>Dropdown bind on view directly  </strong>
                    @Html.DropDownList("ddlBindeInView", new List<SelectListItem>
                                        { new SelectListItem { Text = "Bhimsen", Value

= "1", Selected=true},
                                          new SelectListItem { Text = "Shiv", Value =

"2"},
                                          new SelectListItem { Text = "Ankur", Value =

"3"},
                                          new SelectListItem { Text = "Ravi", Value =

"4"}
                                        }, "Select Mobile", new { @class = "form-

control" })
                </div>
            </div>
            <div class="col-md-6">
                <div class="form-group">
                    <strong>DropDownListFor (Strongly type)  </strong>
                    @Html.DropDownListFor(M => Model.ddlBindBySelectList, new

SelectList(Model.ddlStudentList, "Value", "Text"), new { @class = "form-control" })
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-md-6">
                <div class="form-group">
                    <strong>DropDownList by model  </strong>
                    @Html.DropDownList("ddlmob", Model.ddlStudentList, "Select Model",

new { @class = "form-control" })
                </div>
            </div>
            <div class="col-md-6">
                <div class="form-group">
                    <strong>DropDownList by ViewBag  </strong>
                    @Html.DropDownList("ddlBindByViewBag", ViewBag.UserList as

SelectList, new { @class = "form-control" })

                </div>
            </div>
            <div>
            </div>

        </div>

        <div class="row">
            <div class="col-md-6">
                <strong>DropDownList Option Group  </strong>
                <div class="form-group">
                    @Html.DropDownList("ddlBindByOptGroup", ViewBag.ddlStudentListGrp

as SelectList, new { @class = "form-control" })
                </div>
            </div>
            <div class="col-md-6">
                <div class="form-group">
                    <input type="submit" class="btn btn-primary" />
                </div>
            </div>
            <div>
            </div>

        </div>

    </div>
}


Controller's Action and function

  public ActionResult BindAllDropdown()
        {
            DropdownModel model = new DropdownModel();        
            model.CountryList = BindCountry();
            model.ddlStudentList = new SelectList(BindStudentList(), "Id",

"StudentName");        
            ViewBag.ddlStudentListGrp = new SelectList(BindStudentList(), "Id",

"StudentName", "StudentAddress", 1);
            ViewBag.UserList = new SelectList(BindStudentList(), "Id", "StudentName");

         
            return View(model);
         
        }

 private List<StudentModel> BindStudentList()
        {
            //IQuickQuoteOperation quoteOperation = new QuickQuoteOperation();
            using (TestDBEntities db = new TestDBEntities())
            {
                var lst = db.tbl_Student.ToList();

                List<StudentModel> lstModel = new List<StudentModel>();
                Mapper.Initialize(x => x.CreateMap<tbl_Student, StudentModel>());
                var d = Mapper.Map<IEnumerable<tbl_Student>, List<StudentModel>>(lst);
                return d.ToList();
            }

        }

 public List<Country> BindCountry()
        {
            List<Country> _objcountry = new List<Country>();
            _objcountry.Add(new Country { countryId = 1, CountryName = "India" });
            _objcountry.Add(new Country { countryId = 2, CountryName = "Pakistan" });
            _objcountry.Add(new Country { countryId = 4, CountryName = "Sri Lanka" });
            _objcountry.Add(new Country { countryId = 5, CountryName = "Japan" });
            _objcountry.Add(new Country { countryId = 6, CountryName = "China" });
            return _objcountry;
        }


Model

 public class DropdownModel
    {    
        public SelectList ddlStudentList { get; set; }
        public SelectList ddlStudentListGrp { get; set; }
        public string ddlBindBySelectList { get; set; }
        public List<Country> CountryList { get; set; }
    }


    public class Country
    {
        public int countryId { get; set; }
        public bool CheckedStatus { get; set; }
        public string CountryName { get; set; }
    }


No comments:

Post a Comment