Tuesday, February 26, 2013

Loading dropdownlist in MVC2 and MVC4

Loading doropdownlist in MVC2 and MVC4.


This is the code you will write in Models, here you create a cs file called ProfileModel inside Models folder an write the following code.

public interface IAdminService
    {
        List< tblCountry > GetCountries();
    }


    public class AdminService : IAdminService
    {
        /* this you can cosider as data provider, like any Services, any other assembly etc. */
AdminDTO _adminProvider;
        MasterDTO _masterProvider;

        public AdminService()
        {
            _adminProvider = new AdminDTO();
            _masterProvider = new MasterDTO();
        }


        public List< tblCountry > GetCountries()
        {
            return MasterDTO.GetContries();
        }
    }

now here we see how to call the dataService in controller

public class ProfileController : Controller
    {

        public IDataService dataService { get; set; }

        protected override void Initialize(RequestContext requestContext)
        {
            if dataService == null) { dataService = new DataService(); }

            base.Initialize(requestContext);
        }


/* this is an example of view implementation in example  */
[HttpGet]
        public ActionResult ManageProfile()
        {
            ViewBag.Countries = dataService.GetCountries();
            ViewBag.Message = string.Format("There are {0} contries", dataService.GetCountries().Count);


            List countryList = (from p in
                                                   dataService.GetCountries().ToList()
                                               select new SelectListItem()
                                               {
                                                   Value = p.CountryId.ToString(),
                                                   Text = p.CountryName
                                               }).ToList();

            ViewBag.CountryDropdown = countryList;

            return View();
        }
}

Now your view is ready with data, you just need to display on screen , so here is how you can achieve that.
In "Views" folder you need to create a folder called "Profile", inside the "Profile" folder create a razor page with name ManageProfile, and write the code below.


this is an example of country dorpdown, "CountryDropdown" is nothing but the variable of your view bag
@Html.DropDownList("CountryDropdown")


  this is an example of how the collection object can be displayed like datalist/gridview/collectionbinding
    @{
        var countries = ViewBag.Countries;
    }
    < table >
        @foreach (tblCountry c in countries)
        {
            < tr >
                < td >@c.CountryId
                < /td >
                < td >
                    @Html.ActionLink(c.CountryName, "AddEditState", new { cid = c.CountryId, name = c.CountryName });
                < /td >
                < td >@c.CurrencyCode
                < /td >
                < td >@c.Currency
                < /td >
                < td >@c.Capital
                < /td >
                < td >@c.Population
                < /td >
            < /tr >
        }
    < /table >


 please note : example how to pass parameters with Html.ActionLink()  @Html.ActionLink(c.CountryName, "AddEditState", new { cid = c.CountryId, name = c.CountryName });




No comments:

ETG Consultancy

Web Designing Development Analysis & Promotion
Asp.Net 2.0 SQL WWF WCF SEO Marketing Ajax JQuery NHibernate MVC