how to add a selectlistitem to a selectlist - c#

I have the following select list
public SelectList StaffList { get; set; }
I am assigning it the following,
inspectorCorrespondences.StaffList = new SelectList(userDetails, "Id", "FullName");
but I want to add in another SelectList item
var systemGeneratedUser = new SelectListItem() { Value = "-1", Text = "System Generated" };
how do I do that?

This isn't necessarily the most elegant solution, but you'll need to cast the SelectList's items to a list of SelectListItems if you want to add another SelectListItem.
var myItems = new List<SelectListItem>();
myItems.Add(new SelectListItem() { Text = "AA", Value = "11" } );
var mySelectList = new SelectList(myItems);
((List<SelectListItem>)mySelectList.Items).Add(new SelectListItem() { Text = "BB", Value = "22" });

According to the documentation, it looks like you need to initialize the SelectList with the values instead of adding them. Passing an enumerable of SelectListItem to the constructor appears to be the expected way.
It looks like you're already doing that in the sense that userDetails is already an enumeration? If so, what type of enumeration is it? You may need to add a line or two of code to transform it to an enumeration of SelectListItem objects, add yours to that, and then use that to initialize the SelectList. Maybe something like this:
var listItems = userDetails.Select(u => new SelectListItem { Value = u.Id, Text = u.FullName }).ToList();
listItems.Add(new SelectListItem() { Value = "-1", Text = "System Generated" });
// or perhaps .Insert() at position 0 instead?
inspectorCorrespondences.StaffList = new SelectList(listItems, "Value", "Text");

Related

Enum To SelectList MarkCurrentAsSelected Value Not Working

I have code like this:
model.Availables = enmAvailability.Ava1.ToSelectList(false).ToList();
model.Availables.Insert(0, new SelectListItem { Text = "All", Value = "-1", Selected = true });
In this condition, in the drop down list it should select "All", but "Ava1" comes as selected. What am i doing wrong?

default value for DropDownListFor helper mvc 5

I have this part of code that runs very well:
merge1.Statuses = db.JobStatus.Select(s => new SelectListItem
{
Value = s.JobStatusID.ToString(),
Text = s.JobStatusName
}).ToList();
Now, I want to add a default value that is a dynamic value, depending on the database. So, I tried something like:
private SelectListItem statuses(string defaultStatus)
{
db.JobStatus.Select(s => new SelectListItem
{
Value = s.JobStatusID.ToString(),
Text = s.JobStatusName,
defaultStatus
}).ToList();
return ???
}
So, I have marked red on defaultStatus.
Thanks in advance
I would do it like this
private IEnumerable<SelectListItem> statuses(string defaultStatus)
{
return db.JobStatus.Select(s => new SelectListItem
{
Value = s.JobStatusID.ToString(),
Text = s.JobStatusName,
Selected = s.JobStatusName == defaultStatus
}).ToList();
}
Also change the return type so you can bind it to a select list in the view.

DropDownListFor initial value not selected

I don't understand why the initial value is not selected when the form is loaded.
Here's my code
model.LUIsUsed = new List<SelectListItem>()
{
new SelectListItem { Value = "0", Text = "Ignore" },
new SelectListItem { Value = "1", Text = "Required" },
new SelectListItem { Value = "2", Text = "Optional" },
};
In my view
#Html.DropDownListFor(x => x.Foo[i].IsUsed, Model.LUIsUsed, new { #class= "form-control input-sm is-used" })
x.Foo[i].IsUsed value is "1", but required is not selected. I can't figure out why.
Is it because x.Foo is an array?
EDIT: When I don't use an array, it works
It looks like there's a problem with DropDownListFor inside a for loop.
I had to build a SelectList and specify the selected value.
#Html.DropDownListFor(x => x.Criterias[i].IsUsed, new SelectList(Model.LUIsUsed,"Value","Text", Model.Criterias[i].IsUsed))
It works, but I would like to know why.

How do I get a listbox to multiselect and concatenate string answers? C# MVC

I’m trying to get the listbox to return the strings concatenated and comma separated, returned as a string.
In my ViewModel I have:
public IEnumerable<SelectListItem> XListItems
{
get
{
var list = new List<SelectListItem>();
list.Add(new SelectListItem { Value = "A", Text = "A" });
list.Add(new SelectListItem { Value = "B", Text = "B" });
list.Add(new SelectListItem { Value = "C", Text = "C" });
list.Add(new SelectListItem { Value = "D", Text = "D" });
list.Add(new SelectListItem { Value = "E", Text = "E" });
return list;
}
}
My ViewModel also contains a log which contains AString, which is a String.
In my view I have:
#Html.ListBoxFor(model => model.log.AString, Model.XListItems, new {Multiple = "multiple"})
Right now it is just returning the first item that is selected.
How would I allow the user to select multiple items and have them be returned as a string, such as: "A, C, D"?
Not sure why you would want to return a comma separated string (you would need javascript to construct it). Just change the property AString in you model to string[] AString {get; set;}. The property will then contain an array of the values that have been selected.
If you do need to save the values in that format, then use (in the POST method)
var result = string.Join(",", AString);
And you do not need to include new {Multiple = "multiple"}). ListBoxFor will add that attribute anyway.

How exactly does the selected value of a selectList work?

I need to create a dropdown list and I'm doing this by creating a new SelectList based on a model. Here is how I am trying to do this:
#Html.DropDownListFor(model=>model.SelectedStudio, new SelectList(Model.Studios, "Id", "Name", Model.Studio)
I see from the class signature that the last parameter is object selectedValue. I have now tried many variations, trying to pass a string there, an object, etc. but when I compile the program, the selected value for the dropdown list is never set to what I want.
Can anyone explain how this works?
EDIT: I don't know if this is important, but the Id of Studio is of type ObjectId (from the MongoDB C# Driver)
I think your not setting for the selected value on new SelectList
your code should be
#Html.DropDownListFor(model=>model.SelectedStudio, new SelectList(Model.Studios, "Id", "Name", [Defualt value])
the reason it not give the error becuase it is object. and your setting up your IEnumerable list. so it is object. see the following link
http://msdn.microsoft.com/en-us/library/dd492553(v=vs.118).aspx
Try this:
#Html.DropDownListFor(model=>model.SelectedStudio, new SelectList(Model.Studios, "Id", "Name", Model.Studio.Id)
this is how u keep a value selected in drop down
#Html.DropDownList("statussearch", new List<SelectListItem>
{
new SelectListItem { Text = "text1", Value = "1", Selected = Studio.IsSelected.HasValue ? Studio.IsSelected.Value : false},
new SelectListItem { Text = "text2", Value = "2"},
new SelectListItem { Text = "text3", Value = "3"},
}, "Select Status")
IsSelected is a field in your DB wch states wch value to be selected.
this will work fine for your requirement
#foreach (var value in studios)
{ #Html.DropDownList("statussearch", new List<SelectListItem>
{new SelectListItem { Text =value.prop1, Value = "1" },new SelectListItem { Text = value.prop2, Value = "2"},new SelectListItem { Text = value.prop3, Value = "3"},}, value.proptobeselected)}

Categories