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".
Related
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>
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?
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.
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)}
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.