locked
MVC3 Creating Multiple Dropdowns On One Page RRS feed

  • Question

  • User1628746328 posted

    How do you go about creating a controller that can bind multiple dropdowns (or controls) in a view?

    I do have viewmodel that currently hardcodes a list that looks like this (btw this took me 8 hours):

    public class WebsiteViewModel
        {
            public string website { get; set; }
            public List<SelectListItem> _websites = new List<SelectListItem>();
    
            [Required(ErrorMessage = "Please select a Website.")]
            public string SelectWebsite { get; set; }
    
            public List<SelectListItem> Websites
            {
                get
                {
                    _websites.Add(new SelectListItem() { Text = "", Value = "-1" });
                    _websites.Add(new SelectListItem() { Text = "one", Value = "1" });
                    _websites.Add(new SelectListItem() { Text = "two", Value = "2" });
                   
                    return _websites.ToList();
                }
            }
        }

    The controller does this:

            public ActionResult Index()
            {
                ViewBag.Message = "Welcome to ASP.NET MVC!";
                ViewBag.Categories = new SelectList(dbContext.ViewCatalogs, "CatalogID", "CatalogID");
                return View(new WebsiteViewModel());
            }
    

    The view does this:

    <div>Choose a website</div>
    <div>
        @Html.DropDownList("Websites")
    </div>
    
    

    I still don't understand the whole concept of how these views, controllers and models interact,  or how the html helpers are used to create page controls and how to use them with Lists (with web forms I'm used to dealing with arraylists, datatables and datasets) and what's most frustrating is that there are almost no tutorials to help someone like me coming from webforms, trying to learn MVC3 and showing us how to implment simple things like this.

    It seemed for web forms, finding code samples was pretty easy and it seems to me, the whole MVC3 development environment and community is obfuscating and abstracting what should be simple, straightforward tasks [creating drop downs, filling grids, creating, reading, updating and deleting records] to the point that there's no clear ideas or direction on how to do something as simple as binding a couple of drop down controls and a JQGrid on a page.

    It took me 8 hours to build a simple hard-coded drop down list yesterday and I haven't even gotten to the point where there's any Updating or Creating going on yet...SCARY.  

    On top of it all, I'm trying to incorporate JQGrid with all of this.

    I want to learn this new development platform, however, I'm pretty close to chucking this and going back to webforms.

    Is this progress?

    Because I feel pretty stupid!

    Can anyone help?

    Thanks in advance.

    doug

    Thursday, July 21, 2011 9:43 AM

Answers

  • User197322208 posted

    I want to learn this new development platform, however, I'm pretty close to chucking this and going back to webforms.

    MVC tutorials here http://www.asp.net/mvc/

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 21, 2011 9:59 AM
  • User1642208711 posted

    Sample Model

    public class Person
    {
        public string Name { get; set; }
    
        public string City { get; set; }
    
        public string State { get; set; }
    
        public Dictionary<string, string> States
        {
            get
            {
                return GetStateList();
            }
        }
    
        public string[] Cities {
            get
            {
                return GetCityList();
            }
        }
    
        private Dictionary<string, string> GetStateList()
        {
            var stList = new Dictionary<string, string>();
            stList.Add("KR", "Kerala");
            stList.Add("AR", "Andhra");
            stList.Add("MU", "Mumbai");
            return stList;
        }
    
        private string[] GetCityList()
        {
            return new string[] { "Shilong", "Shimla", "Delhi", "Ernakulam" };
        }
    }

     Sample Controller

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var person = new Person();
            ViewBag.States = new SelectList(person.States, "key", "value", person.State); // Here person.State is to set the selected value for DDL
            ViewBag.Cities = new SelectList(person.Cities, person.City);
            return View(person);
        }
    
        [HttpPost]
        public ActionResult Index(Person person)
        {
            /* MVC will create a new instance of model based on the current values in the view and pass as parameter to this method
             * Also person.State returns the selected value from DDL
            */
            ViewBag.States = new SelectList(person.States, "key", "value", person.State);
            ViewBag.Cities = new SelectList(person.Cities, person.City);
            return View(person);
        }
    }

    Sample View

    @model MvcApplication1.Models.Person
    
    @{
        ViewBag.Title = "Index";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }
    
    @using (Html.BeginForm())
    {
        @Html.DropDownListFor(m => m.State, (SelectList)ViewBag.States) //Here m.State is for setting the selected value to State property
        @Html.DropDownListFor(m => m.City, (SelectList)ViewBag.Cities) //Here m.City is for setting the selected value to City property
        <input type="submit" value="Submit" />
    }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 21, 2011 11:18 AM
  • User-1005219520 posted

    Getting Started With MVC3
    Getting Started with EF using MVC

    In the short term, most folks are more productive with Web Forms.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 21, 2011 1:01 PM
  • User-1005219520 posted

    so you're saying, as soon as I get over the learning curve [sometime in 2013] i'll be more productive?

    Obvioulsy, it's much more complicated than that. Smaller LOB apps are often more productive with Web Forms. For some folks, the MVC architertural pattern is too complext to ever be productive.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 21, 2011 3:48 PM

All replies

  • User197322208 posted

    I want to learn this new development platform, however, I'm pretty close to chucking this and going back to webforms.

    MVC tutorials here http://www.asp.net/mvc/

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 21, 2011 9:59 AM
  • User1628746328 posted

    here's the question again:

    How do you go about creating a controller that can bind multiple dropdowns (or controls) in a view?

    Thursday, July 21, 2011 10:44 AM
  • User1642208711 posted

    Sample Model

    public class Person
    {
        public string Name { get; set; }
    
        public string City { get; set; }
    
        public string State { get; set; }
    
        public Dictionary<string, string> States
        {
            get
            {
                return GetStateList();
            }
        }
    
        public string[] Cities {
            get
            {
                return GetCityList();
            }
        }
    
        private Dictionary<string, string> GetStateList()
        {
            var stList = new Dictionary<string, string>();
            stList.Add("KR", "Kerala");
            stList.Add("AR", "Andhra");
            stList.Add("MU", "Mumbai");
            return stList;
        }
    
        private string[] GetCityList()
        {
            return new string[] { "Shilong", "Shimla", "Delhi", "Ernakulam" };
        }
    }

     Sample Controller

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var person = new Person();
            ViewBag.States = new SelectList(person.States, "key", "value", person.State); // Here person.State is to set the selected value for DDL
            ViewBag.Cities = new SelectList(person.Cities, person.City);
            return View(person);
        }
    
        [HttpPost]
        public ActionResult Index(Person person)
        {
            /* MVC will create a new instance of model based on the current values in the view and pass as parameter to this method
             * Also person.State returns the selected value from DDL
            */
            ViewBag.States = new SelectList(person.States, "key", "value", person.State);
            ViewBag.Cities = new SelectList(person.Cities, person.City);
            return View(person);
        }
    }

    Sample View

    @model MvcApplication1.Models.Person
    
    @{
        ViewBag.Title = "Index";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }
    
    @using (Html.BeginForm())
    {
        @Html.DropDownListFor(m => m.State, (SelectList)ViewBag.States) //Here m.State is for setting the selected value to State property
        @Html.DropDownListFor(m => m.City, (SelectList)ViewBag.Cities) //Here m.City is for setting the selected value to City property
        <input type="submit" value="Submit" />
    }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 21, 2011 11:18 AM
  • User-1005219520 posted

    Getting Started With MVC3
    Getting Started with EF using MVC

    In the short term, most folks are more productive with Web Forms.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 21, 2011 1:01 PM
  • User1628746328 posted

    In the short term, most folks are more productive with Web Forms.

    so you're saying, as soon as I get over the learning curve [sometime in 2013] i'll be more productive?

    Thursday, July 21, 2011 1:11 PM
  • User197322208 posted
    2013...how much time do you have for learning?
    Thursday, July 21, 2011 2:47 PM
  • User-1005219520 posted

    so you're saying, as soon as I get over the learning curve [sometime in 2013] i'll be more productive?

    Obvioulsy, it's much more complicated than that. Smaller LOB apps are often more productive with Web Forms. For some folks, the MVC architertural pattern is too complext to ever be productive.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 21, 2011 3:48 PM