Enum To SelectList MarkCurrentAsSelected Value Not Working - c#

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?

Related

How to make MVC dropdownlist change editor field value

I have an edit view that implements a normal editor text field. The user can manually enter a value that can be saved to the model. However, alongside this editor field I also need a dropdownlist that the user can select from that will edit the editor field. However, the value I am putting in the editor field is not the same as the value selected from the dropdownlist, this value comes from another list which uses the index of the item selected by the dropdownlist to put the value. To illustrate:
I have a text field "Name Id" and and two lists "Name" and "Id". The user will select a "Name" and the view will place the "Id" into the "Name Id" Field.
Example:
Name Id: 2 <----- 2 was entered because Bob was selected
Name:
Alex
Bob
Carl
Dennis
Note: Don't forget to include JQuery reference for js code.
Razor Code:
#{
var domainsList = new SelectList(new []
{
new SelectListItem { Text = "Please Select", Value = "0", Selected = true },
new SelectListItem { Text = "Alex", Value = "1" },
new SelectListItem { Text = "Bob", Value = "2"},
new SelectListItem { Text = "Carl", Value = "3"},
new SelectListItem { Text = "Dennis", Value = "4"}
}, "Value", "Text");
}
#Html.Editor("NameId", new {#class="form-control"})
#Html.DropDownList("Nameslst", domainsList,new {#class="form-control"})
Js Code
<script>
$(function(){
$('#Nameslst').on('change',function(){
$('#NameId').val($(this).val())
})
})
</script>

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)}

Turning bit type into a dropdownlistfor

I have a BIT type in a sql database that I want to turn into a DropDownList.
I pass a model to the page which has an active field. This field is either true or false. I'm adding the ability to modify this column in the database.
Is it possible to create a DropDownListFor that will give them the option of "Active" "Disable" and then return the correct value to the database?
#Html.DropDownListFor(m => m.active, ??)
#Model.active
I have never tried this but how about this:
#Html.DropDownListFor(m=> m.active, new List<SelectListItem>() { new SelectListItem { Text = "Active", Value = "true", Selected = Model.active }, new SelectListItem { Text = "Inactive", Value = "false", Selected = Model.active } })
This solution works great. The only thing I had to change for my code is the "Selected" value if the model value is "False"
#Html.DropDownListFor(m=> m.active, new List<SelectListItem>() { new SelectListItem { Text = "Active", Value = "true", Selected = Model.active }, new SelectListItem { Text = "Inactive", Value = "false", Selected = !Model.active } })
It was displaying Active even though it should display Inactive.

how to add a selectlistitem to a selectlist

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");

DropdownList Not Showing Selected Item

I am creating a page to edit non-conformance reports. The page takes in a model which is as follows:
On the page, I am trying to show a dropdown list with the current value of the FaultCode property pre-selected. However, it is not working. Instead I have stripped it back to the basics and tried to hard code a pre-selected value, as follows:
#{
var faultCodeList = new List<SelectListItem>
{
new SelectListItem { Text = "Internal Damage", Value = "Internal Damage" },
new SelectListItem { Text = "Customer Problem", Value = "Customer Problem" },
new SelectListItem { Text = "Incorrect Material Supplied", Value = "Incorrect Material Supplied" },
new SelectListItem { Text = "Received Material Damaged", Value = "Received Material Damaged" },
new SelectListItem { Text = "No Test Certificate Supplied", Value = "No Test Certificate Supplied" },
new SelectListItem { Text = "Other", Value = "Other", Selected = true}
};
}
#Html.DropDownList("FaultCode", faultCodeList)
This still does not work. However, if I change the name of the field to something other than FaultCode - i.e FaultCode2 - it works fine!
Why am I seeing this strange behaviour? On the same page I have dropdown lists for other fields, and they are working perfectly...
#Html.DropdownList("FaultCode", new SelectList(faultCodeList, "Value", "Text","Other"))
You need to bind to SelectList than List, so that last parameter you can mention the selected value is "Other".

Categories