Drop down list with already defined Values MVC2 - asp.net-mvc

I am trying to create a pre-defined drop down list for C# ASP.NET MVC2. I am way confused on how to get this done... Boy did we have it easy in vanilla asp.net...
But, let say I wanted the drop down list to have simple text like ... BMW, Audi, Mercedes, and values such as 1, 2, 3 or even "good", "better", "Best"... How would I create the Drop down list in the controller and how would I get it to the View?
What I have right now is
List<SelectListItem> MRNList = new List<SelectListItem>
{
new SelectListItem { Text = "BMW", Value = "good", Selected = false},
new SelectListItem { Text = "Audi", Value = "better"},
new SelectListItem { Text = "Mercedes", Value = "Best"},
};
ViewData["MRNDROPDOWNLIST"] = MRNList.AsEnumerable();.
In my view I have...
<%:Html.DropDownListfor("MRN", ViewData["MRNDROPDOWNLIST"]) %>
What am I missing here? I am getting a compile time error from Visual Studio telling me that I can't do that? But the Parameter for DropDownList definitely says that you can use An IEnumerable list to fill that param? I know I am missing something obvious here... Not sure what it is just yet... I will keep looking, but if any of you have any clues I would definitely appreciate it.
Derek

ViewData["MRNDROPDOWNLIST"] is of type object and you will need to cast that to the correct type for the error to go away, which in this case would be to List<SelectListItem>:
<%:Html.DropDownListfor("MRN", (List<SelectListItem>)ViewData["MRNDROPDOWNLIST"])%>

Related

dynamic dropdownlist in ASP.net Razor MVC5 from a Many to many database

Here is the look of my tables in MSSQL MS :
MSSQL Management Studio
As i’ve learned, Entity Framework is changing the schema to hide the middle database and made it look like this :
VB view with Entity Framework
I dont really know how to use the middle table (and if i should use it or not).
The idea of my website is to be able to create a student (table etudiant) with multiples cursus (table course).
When i create a student (etudiant), I should be able to pick up for example 5 cursus (cours). By adding a dropdown list and a + button to make another dropdownlist appears.
When i look at the detail of a student, i should see all the curses he has choosen.
Thanks for you help !
Yes you need to use a "middle table" (called etuCours in your diagram). This is the correct way of handling a many to many relation.
You probably don't need mulitple dynamic dropdowns to select multiple course, i would implement a multi select "drop down" which is more of a list.
Have a look at http://www.w3schools.com/tags/att_select_multiple.asp
There also many options available using jQuery if you want it to look pretty. Like http://loudev.com/
Using Razor it would be:
#{
var foo = new List<SelectListItem> {
new SelectListItem { Text = "Foo 1", Value = "1", Selected = true },
new SelectListItem { Text = "Foo 2", Value = "2" },
new SelectListItem { Text = "Foo 3", Value = "3", Selected = true }
};
}
#Html.ListBox("foo", foo)

I'm having troubles getting drop-down-menus in MVC to return data

I am very new to MVC as a whole, and I was sent to create a web application that would give the user options then change the view based on these options. First I created a simple "select" html drop down menu but I am under the assumption that this won't work.
I can supply all the actual code I currently have, I just don't know what would be important to see and what would just be bothersome to wade through.
When I had created a constructor for the model, it wouldn't go through that constructor or else it would be easier.
Sorry since this all sounds probably bad, but any help would be amazing.
There are couple ways to do that , here's what i do :
create the function in your controller (The one that returns the list of items you want to include in the dropdownmenu) . let's name it getNames();
Then return that to the view using #viwbag.Names;
In the view you can declare a variable , something like:
#{
SelectListItem[] Names = ViewBag.Name;
}
and the dropdown menu code should be something like this:
#Html.DropDownList("Names", Names)
Finally, here is sample code for the controller:
IEnumerable<SelectListItem> Names =
(from names in YourDB.students.ToArray()
select new SelectListItem
{
Text = names.Name,
Value = names.Id
}).ToArray();
var Templist = names.ToList();
ViewBag.Roles = Templist.ToArray();

Dropdown in ASP.NET MVC 3

I am new to MVC so probably confused . can somebody please explain me the dropdown in razor.my questions are-
What is the difference in dropdownlist and dropdownlistfor
how do i Pass ID column of my database table as value and NAME column as text.
How do i add "other" to the dropdownlist.
how do i access the selectedlistitem in code behind.
if possible please explain with an example.
DropDownList is generated by code like this:
#Html.DropDownList("PersonId", new SelectList(Model.People, "Id", "Text");
On the other hand, DropDownListFor is generated like this:
#Html.DropDownListFor(m => m.PersonId, new SelectList(Model.People, "Id", "Text")
Problem with DropDownList is that it has a magic string and if you decide to refactor the model later on, there's a high change you'll forget to change the magic string too.
You could do a LINQ query like this:
var datalist = New SelectList(from x in _peopleService
select new SelectListItem { Text = x.Name, Value = x.Id});
If you don't have a service or an ORM between it you need to apply it to your situation, but you can generate a list like that.
After nr 2, you can
datalist.Add(new SelectListItem() { Text = "Other", Value = "-1"});
Also you have to put that datalist in your viewmodel/model that is passed to the View, so you can generate a selectlist item with that. In this case you could just do:
#Html.DropDownListFor(x => x.PersonId, Model.PersonList);
if you stored the list as PersonList in Model.
In your Viewmodel (Well, model in mvc) you have a property where the selected item will be stored, look at the 1st question - In this instance the selected item's id will be stored in the PersonId property.

DropDownListFor Not Displaying/Persisting Null for First Choice in List

I have an MVC app via SharpArch. In the view I have:
Html.DropDownListFor(x => x.Location,
new SelectList(Model.PossibleLocations, "Id", "Address"),
"-- No Location --")
I have 2 issues.
The Dropdown is not getting updated when the view gets bound to the model.
A selection gets persisted correctly except when I try the top "no location".
I was able to take care of the first point by changing x.Location to x.Location.Id but then I had other issues.
I can find plenty of examples for DropDownList, but none in which saving a null is shown.
Any help is appreciated.
Thanks
UPDATE:
I just upgraded Resharper (a minor update) and am getting prompted to fill it DropDownListFor. Why would that make a difference? It was working enough to bind and now it doesn't work at all.
You could always insert the "empty" SelectListItem into your PossibleLocations collection before passing to the View and check for that "empty" value (0 for instance) when the form is posted
model.PossibleLocations.Insert(0,
new SelectListItem() { Text = "-- No Location --", Value = "0" };
Try "flattening" your model class so that LocationId is the actual value you're binding to (i.e. add a "LocationId" property to your model, and create a DropDownList for x => x.LocationId).

Help with asp.net mvc select dropdown

I have a marital status field in my users table that its just varchar
yet I only want to give the users four options (married, single, widowed and divorced) and i want to have the correct one selected when Im editing the form.. is it possible? please help.
This should point you in the right direction:
<%= Html.DropDownList("listName", new string[] { "Married", "Single", "Widowed", "Divorced" }
.Select(m => new SelectListItem(){
Selected = model.MaritalStatus == m,
Text = m,
Value = m
})); %>
Assuming that your model has a 'MaritalStatus' field,
Selected = model.MaritalStatus == m
will select the status of your model by default.
This blog post should lead you in the right direction:
http://weblogs.asp.net/ashicmahtab/archive/2009/03/27/asp-net-mvc-html-dropdownlist-and-selected-value.aspx
you'd have to give us a little more information and maybe a code sample of what you have so far to get a more specific answer.

Resources