Dropdown not selecting correct value - c#

what is wrong withe following code?
#Html.DropDownListFor(model => model.title, new List<SelectListItem>
{
new SelectListItem { Text = "Other" , Value = "Other"},
new SelectListItem { Text = "Mr", Value = "Mr" },
new SelectListItem { Text = "Mrs", Value = "Mrs" },
new SelectListItem { Text = "Ms", Value = "Ms" },
new SelectListItem { Text = "Miss", Value = "Miss" }
},
new { #class = "form-control" })
the above is allowing me to select and save the value to the table, but when it come to edit , the saved value is not selected
for example the existing Data was saved with "Mr" when editing it shows "Other"
why?

Try to use SelectList with this overload following way:
#Html.DropDownListFor(model => model.title, new SelectList(new List<SelectListItem>
{
new SelectListItem { Text = "Other" , Value = "Other"},
new SelectListItem { Text = "Mr", Value = "Mr" },
new SelectListItem { Text = "Mrs", Value = "Mrs" },
new SelectListItem { Text = "Ms", Value = "Ms" },
new SelectListItem { Text = "Miss", Value = "Miss" }
},
"Value",
"Text",
Model.Title),
new { #class = "form-control" })
SelectList Constructor (IEnumerable, String, String, String, Object)
public SelectList(
IEnumerable items,
string dataValueField,
string dataTextField,
string dataGroupField,
Object selectedValue
)

this is worst
it is working in this cause but not the one above
#Html.DropDownListFor(model => model.PreferredContact, new List<SelectListItem>
{
new SelectListItem { Text = "Other" },
new SelectListItem { Text = "Telephone", Value = "Telephone" } ,
new SelectListItem { Text = "Email", Value = "Email" },
new SelectListItem { Text = "Post", Value = "Post" }
}, new { #class = "form-control" })

okay
i found the issue
you cannot have "title" as the name. title is a property on HTML5. i renamed the field to dTitle and it works

Related

Create c# SelectList Without specifying values

When manually creating a select list I normally have both text & value:
SelectList SupplierStatusList = new SelectList(new[] {
new SelectListItem { Selected = false, Text = "Please Select", Value = "Please Select" },
new SelectListItem { Selected = true, Text = "1", Value = "1" },
new SelectListItem { Selected = false, Text = "2", Value = "2" },
new SelectListItem { Selected = false, Text = "3", Value = "3" },
new SelectListItem { Selected = false, Text = "4", Value = "4" },
}, "Value", "Text");
But how do I do this without having to specify the value? This leaves the values = "" which I don't want.
SelectList SupplierStatusList = new SelectList(new[] {
new SelectListItem { Selected = false, Text = "Please Select"},
new SelectListItem { Selected = true, Text = "1"},
new SelectListItem { Selected = false, Text = "2"},
new SelectListItem { Selected = false, Text = "3"},
new SelectListItem { Selected = false, Text = "4"},
}, "Value", "Text");
Any ideas?
Sorry, to be clear yes I would like the text and value to be the same.
As suggested in the comments, you can use a constructor overload:
var selectListItems = new[] {"Please Select", "1", "2", "3"};
var selectList = new SelectList(selectListItems, "1");

Dropdown Selected item working in DropDownList but not in DropDownListFor - MVC

I am not able to identify why DropDownListFor is not selecting the correct option. Below is my code, I have hard-coded 'Selected' values for testing but still when page loads I am getting 'Select Country' as selected value.
#Html.DropDownListFor(x => x.CountryID,
new List<SelectListItem> {
new SelectListItem { Text = "USA", Value = "US", Selected = false},
new SelectListItem { Text = "UK", Value = "UK", Selected = true},
new SelectListItem { Text = "Australia", Value = "AUS", Selected = false }
},"Select Country")
But, same is working fine for DropDownList!
#Html.DropDownList("CountryID",
new List<SelectListItem> {
new SelectListItem { Text = "USA", Value = "US", Selected = false},
new SelectListItem { Text = "UK", Value = "UK", Selected = true},
new SelectListItem { Text = "Australia", Value = "AUS", Selected = false }
},"Select Country")
Please Help.
The selected option will be based on the actual value of property CountryID when you use the strongly type helper. So if you set CountryID="UK" in the controller before you pass the model to the view, then the second option will be selected.
Try the following:
#{
var listItems = new List<SelectListItem>
{
new SelectListItem { Text = "USA", Value = "USA" },
new SelectListItem { Text = "UK", Value = "UK", Selected = true },
new SelectListItem { Text = "Australia", Value = "AUS" }
};
}
#Html.DropDownListFor(x => x.CountryID, listItems, "-- Select Country --")

No ViewData Item of type 'IEnumerable<SelectListItem>' that has the key xxxxxx

I have created a dropdown list in ASP.NET MVC 5 and upon submission I get the above error.
Controller code:
public ActionResult Create()
{
List<SelectListItem> items = new List<SelectListItem>();
items.Add(new SelectListItem { Text = "No", Value = "0" });
items.Add(new SelectListItem { Text = "Yes", Value = "1" });
items.Add(new SelectListItem { Text = "Unconfirmed", Value = "null" });
ViewBag.DropDown = items;
return View();
}
View code:
#Html.DropDownListFor(model => model.VisitRequested, (IEnumerable<SelectListItem>)ViewBag.Dropdown, new { onClick = "showHide();" })
How would one fix this?
Replace below
#Html.DropDownListFor(model => model.VisitRequested,
(IEnumerable<SelectListItem>)ViewBag.Dropdown,
new { onClick = "showHide();" })
With
#Html.DropDownListFor(model => model.VisitRequested,
new SelectList(ViewBag.Dropdown, "Value", "Text"),
new { onClick = "showHide();" })
This will fix your issue !!
I tried this and it worked for me
public ActionResult Create()
{
var items = new List<SelectListItem>();
items.Add(new SelectListItem { Text = "No", Value = "0" });
items.Add(new SelectListItem { Text = "Yes", Value = "1" });
items.Add(new SelectListItem { Text = "Unconfirmed", Value = "null" });
ViewBag.DropDown = items;
return View();
}
with this
#Html.DropDownListFor(model => model.VisitRequested,
new SelectList(ViewBag.Dropdown, "Value", "Text"),
new { onClick = "showHide();" })

Multiselect chosen dropdown - want to set values

Please let me know how can I set the selected value in the chosen multiselect dropdownlist. I have tried everything available on net but nothing is working.
My view is
#Html.DropDownListFor(
model => model.SelectedSkills,
new SelectList(
Model.SkillList,
"Value",
"Text",
Model.SelectedSkills
),
new {
#class = "chosen",
id = "ddlSkill",
name="ddlPersonSkill",
#tabindex = "4",
multiple = "true"
}
)
#Html.HiddenFor(
model => model.SelectedSkills,
new { id = "hdnPersonSkill" }
)
Now in page load I need to display the selected value :
var personskill = $("#hdnPersonSkill").val();
$('#ddlSkill option:nth-child(3)').attr('selected', 'selected');
$('#ddlSkill').trigger('liszt:updated');
But above code is not working
CHHTML:
#Html.ListBoxFor(
model => model.SelectedSkills,
new SelectList(
Model.SkillList,
"Value",
"Text",
Model.SelectedSkills
),
new
{
#class = "chosen",
id = "ddlSkill",
name = "ddlPersonSkill",
#tabindex = "4",
multiple = "true"
}
)
Javascript to show all selected:
$(document).ready(function () {
var conceptName = $('#ddlSkill').find(":selected").text();
alert(conceptName);
});
Model jsut for example:
public string[] SelectedSkills { get; set; }
public IEnumerable<SelectListItem> SkillList { get; set; }
public MyModel()
{
SkillList = new List<SelectListItem>();
((List<SelectListItem>)SkillList).Add(new SelectListItem { Text = "Skill 1", Value = "1" });
((List<SelectListItem>)SkillList).Add(new SelectListItem { Text = "Skill 2", Value = "2" });
((List<SelectListItem>)SkillList).Add(new SelectListItem { Text = "Skill 3", Value = "3" });
((List<SelectListItem>)SkillList).Add(new SelectListItem { Text = "Skill 4", Value = "4" });
SelectedSkills = new string[3];
SelectedSkills[0] = "1";
SelectedSkills[1] = "2";
SelectedSkills[2] = "3";
}

SelectItem is not selecting item

View (RelacionamentoConvidado.cshtml)
#Html.DropDownList("Foi_Emitido", (IEnumerable<SelectListItem>)ViewBag.Foi_Emitido, #SRSVP.Util.Constante.HTML_HELPER_DROPDOWN_EMPTY_VALUE, new { #class = "input-small" })
Controller (EventoConvidadoController)
ViewBag.Foi_Emitido = new SelectList(Common.SimNao(model.foi_emitido), "Value", "Text");
Common.cs (Static Class)
public static List<SelectListItem> SimNao(object selectedItem)
{
List<SelectListItem> _returnList = new List<SelectListItem>();
SelectListItem _mList = new SelectListItem();
_mList = new SelectListItem() { Text = "Sim", Value = "true", Selected = selectedItem == null ? false : selectedItem.ToString().Equals("true") };
_returnList.Add(_mList);
_mList = new SelectListItem() { Text = "Não", Value = "false", Selected = selectedItem == null ? false : selectedItem.ToString().Equals("false") };
_returnList.Add(_mList);
return _returnList;
}
When the page is loaded select item is not select item that return from my database.
How can I do this?
This Often happens when the Select List and bound variable share the same name --Foi_Emitido. Try renaming the ViewBag SelectList
#Html.DropDownList("Foi_Emitido", (IEnumerable<SelectListItem>)ViewBag.Foi_Emitido_SelectList, #SRSVP.Util.Constante.HTML_HELPER_DROPDOWN_EMPTY_VALUE, new { #class = "input-small" })
Controller (EventoConvidadoController)
ViewBag.Foi_Emitido_SelectList= new SelectList(Common.SimNao(model.foi_emitido), "Value", "Text");

Categories

Resources