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.
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" />
}
Please remember to click 'Mark as Answer', if this post helps you.
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.
drdexter33
Contributor
2289 Points
889 Posts
MVC3 Creating Multiple Dropdowns On One Page
Jul 21, 2011 01:43 PM|LINK
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
ignatandrei
All-Star
137716 Points
22159 Posts
Moderator
MVP
Re: MVC3 Creating Multiple Dropdowns On One Page
Jul 21, 2011 01:59 PM|LINK
MVC tutorials here http://www.asp.net/mvc/
drdexter33
Contributor
2289 Points
889 Posts
Re: MVC3 Creating Multiple Dropdowns On One Page
Jul 21, 2011 02:44 PM|LINK
here's the question again:
How do you go about creating a controller that can bind multiple dropdowns (or controls) in a view?
Raja Boopath...
Contributor
2427 Points
369 Posts
Re: MVC3 Creating Multiple Dropdowns On One Page
Jul 21, 2011 03:18 PM|LINK
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" /> }ricka6
All-Star
15088 Points
2277 Posts
Microsoft
Moderator
Re: MVC3 Creating Multiple Dropdowns On One Page
Jul 21, 2011 05:01 PM|LINK
Getting Started With MVC3
Getting Started with EF using MVC
In the short term, most folks are more productive with Web Forms.
drdexter33
Contributor
2289 Points
889 Posts
Re: MVC3 Creating Multiple Dropdowns On One Page
Jul 21, 2011 05:11 PM|LINK
so you're saying, as soon as I get over the learning curve [sometime in 2013] i'll be more productive?
ignatandrei
All-Star
137716 Points
22159 Posts
Moderator
MVP
Re: MVC3 Creating Multiple Dropdowns On One Page
Jul 21, 2011 06:47 PM|LINK
ricka6
All-Star
15088 Points
2277 Posts
Microsoft
Moderator
Re: MVC3 Creating Multiple Dropdowns On One Page
Jul 21, 2011 07:48 PM|LINK
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.