Setting a default selected value in DropDownList in MVC3 - asp.net-mvc-3

In MVC3 I have this code at my controller. It retrieves a List of IDs\Names from Installation table and creates a ViewBag
var vs = dba.Installation.OrderBy(q => q.InstName).ToList();
ViewBag.Vessels = new SelectList(vs, "InstId", "InstName");
Now, at my view. I want to render the list in a dropdown list. I used the Html helper that works fine...
#Html.DropDownList("InstId",(SelectList)ViewBag.Vessels, "- Select one -")
I need to set first item in the ViewBag List as a default selected value, instead of "- Select one -" text.
How can I do it?
Thanks in advance!

There's an overload for the SelectList constructor that takes 4 arguments. The last of which is the default selected object. E.g:
ViewBag.Vessels = new SelectList(vs, "InstId", "InstName", selectedValue);
Where selectedValue is an object of whatever type is in your list.

I need to set first item in the ViewBag List as a default selected
value, instead of "- Select one -" text.
Then you need to select the first item in your list (vs) and get it's id and use it as the selecedValue in the SelectList:
ViewBag.Vessels = new SelectList(vs, "InstId", "InstName",
vs.FirstOrDefault().InstId);

You can also create an Helper class for Dropdownlist
It will have a method to set the text as by default text for every dropdownlist in you solution

Related

How to get attribute value of mutiple selection in the Owebia shipping method

How to get attribute value of multiple selection in the Owebia shipping method?
My Owebia code below:
{count items where array_match_any(product.attribute.**shipping_restriction**.**value**', array('AU','NZ','AU,NZ'))}
But,I can't get the attribute value,I only can get the id num of attribute,
shipping_restriction is my custom attribute,which has a multiple selected menu.
It just return the index num,not 'NZ' or 'AU',it just return the index num,not 'NZ' or 'AU'
If I have changed the multiple selected menu into single selection,it could get right value. Like 'NZ' or 'AU',and not index num.
You can try by using following syntax to get value.
product.attribute.*.value
Where * is attribute placeholder.
Reference: http://www.owebia.com/os2/en/doc#formulas_variables_product

assign list items which are attributes in a viewmodel to table attributes in asp.net mvc3

I am using Asp.net mvc3
In my viewmodel I have taken an IEnumerable attributelike:
public IEnumerable<EmployeeDetailsViewModel> EmployeeList{get;set;}
In my controller this EmployeeList is having a list of 2*2 matrix. i.e. I have passed an array to this list.
My array name is :employee
attributes:
First Name
Last Name
Task
after result it is displaying employee list in EmployeeList like EmployeeList[0]: values of First Name, Last Name,Task
now I want to store the list values in table in database.
My table also contains similar fields like my array attributes
I am using sql Server 2008 as backend
I want to do this in my controller.
how can I do this?
do I need to use foreach loop to assign list values to attributes of table or need to do something else?
please suggest me something...
I myself resolved the problem:
can use foreach loop here like:
foreach (ModelName item in viewModel.EmployeeList)
{
TableName a = new TableName();
a.Project1up = item.FirstName;
a.Employee1up = item.LastName;
a.Allocation = item.Task;
}

Populating dropdown list with two columns from database

I'm working on MVC3 project.
At my controller I already have a code that gets the Descr column from StockClass table. After that I fill a ViewBag with this list in order to retrieve it from the view and populate the dropdown list.
Currently is working fine but only shows Descr field (obviously). What i want is populate the dropdown list with two fields (Code and Descr) in this format: "code - descr".
I tried several ways but i cannot find the way to code the #Html helper correctly.
In my controller...
var oc = dba.StockClass.OrderBy(q => q.Code).ToList();
ViewBag.OrderClass = new SelectList(oc, "StockClassId", "Descr");
In my view....
#Html.DropDownList("StockClassID", (SelectList)ViewBag.OrderClass)
Could you please help me?
I don't believe there is an HTML Helper which will do that for you, but you can get what you're after like this:
var oc = dba.StockClass
.OrderBy(q => q.Code)
.ToDictionary(q => q.StockClassId, q => q.Code + " - " + q.Descr);
ViewBag.OrderClass = new SelectList(oc, "Key", "Value");
This also has the advantage of making the uses of StockClassId, Code and Descr refactor-friendly - if you rename those properties you won't be able to compile without updating this bit of code to match.

Wicket DropDownChoice setting model value

I have a table (Dataview) with content from a database, where each row/object has an "edit" button. When I try to edit the object, the DropDownChoice-value (in a form) is not correctly updated (even though it is correct in the database). The value that gets set in the DDC is the first item in the (sorted) list "placeList", where I obviously want the correct value from my object (event.getPlace().getName()).
Here is the code (wicket 1.5):
List<Place> placesList = UtGuidenApplication.getInstance().getUgpService().getAllPlaces();
Collections.sort(placesList);
DropDownChoice<Place> selectablePlaceField = new DropDownChoice<Place>("Sted", new PropertyModel<Place>(event, "eventPlace.name"),
placesList, new ChoiceRenderer<Place>("name"));
utguidenEventForm.add(selectablePlaceField);
Anybody?
Cheers,
Terje Eithun,
Norway
I think you have an error in your model. You've written new PropertyModel<Place>(event, "eventPlace.name") which contains the name of your event as model, but the list of choices contains places. I think using new PropertyModel<Place>(event, "eventPlace") should solve the issue.

WatiN SelectList - How can I select any element from the list?

I'm using WatiN to do some web testing and have run into a problem with a select list.
I have to run through a few pages first, adding a 'Category' element that will populate said troublesome select list.
I have been able to easily select an element from the list using ByValue, but the problem here is that the values is more of an index element that is created on the fly with a seemingly random value when the 'Category' element is created. I have tried to use the text that is under the option list in the html but it cannot seem to find it.
At this point I'm willing to settle for any element in the list that isn't value "-1" as this is the "Please select an option item".
Any help at all would be appreciated,
Thanks in advance
Keith 8o8
If i understand correctly maybe this can help:
OptionCollection options = browser.SelectList("elementId").Options;
options[0].Select();
Here is a solution :
SelectList list = _browser.Frame(Find.ById(frameId)).SelectList(listId);
foreach (Option tempOption in list.Options)
{
string value = tempOption.Text;
if (!string.IsNullOrEmpty(value)) // Compare with visible option text
{
if (value.Equals(option))
{
list.Option(option).Select();
found = true;

Resources