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");
Related
This question already has answers here:
MVC5 - How to set "selectedValue" in DropDownListFor Html helper
(5 answers)
Closed 5 years ago.
I have a IEnumerable<SelectListItem>
My ViewModel
public class StatusClass
{
public string Status { get; set; }
public IEnumerable<SelectListItem> StatusList { get; set; }
}
I set values in to StatusList from my controller.
StatusClass statusObj = new CRM.StatusClass();
List<SelectListItem> Discountdata = new List<SelectListItem>();
Discountdata.Add(new SelectListItem() { Value = "All", Text = "All" });
Discountdata.Add(new SelectListItem() { Value = "Draft", Text = "Draft" });
Discountdata.Add(new SelectListItem() { Value = "Issued", Text = "Issued" });
Discountdata.Add(new SelectListItem() { Value = "Partial", Text = "Partially Received" });
Discountdata.Add(new SelectListItem() { Value = "Received", Text = "Received" });
Discountdata.Add(new SelectListItem() { Value = "PAID", Text = "Paid" });
Discountdata.Add(new SelectListItem() { Value = "Billed", Text = "Billed" });
statusObj.StatusList = new SelectList(Discountdata, "Value", "Text");
This works fine and my HTML is like this:
#Html.DropDownListFor(model => model.Status, Model.StatusList)
What i want is, I need to set the selected value from the controller when the list get created.
Suppose I have a string like this:
string newwStatus = "Issued";
How can I set it as selected in the SelectListItem.
I tried this, but its not working in my case:
foreach(var item in StatusList)
{
if(item.value == status)
{
item.Selected = true;
}
}
and tries this too:
IEnumerable<SelectListItem> selectList =
from s in StatusList
select new SelectListItem
{
Selected = (s.Value == status),
Text = s.Text,
Value = s.Value
};
I dont know if these are the right way if someone know how to do this, please help.
Thanks in advance.
The Selected property of SelectListItem is ignored when binding to a model property. You need to set the value of your Status property in the GET method before you pass the model to the view
List<SelectListItem> Discountdata = new List<SelectListItem>
{
new SelectListItem() { Value = "All", Text = "All" },
new SelectListItem() { Value = "Draft", Text = "Draft" },
new SelectListItem() { Value = "Issued", Text = "Issued" },
....
};
StatusClass model = new CRM.StatusClass
{
StatusList = Discountdata,
Status = "Issued"
};
return View(model);
and the 2nd option in your <select> element will be selected.
Note that Discountdata is already IEnumerable<SelectListItem> and using new SelectList(Discountdata, "Value", "Text") to create an identical IEnumerable<SelectListItem> is unnecessary extra overhead.
Note also that since you have the same value for both the value attribute and display text, you could simply use
List<string> Discountdata = new List<string>{ "All", "Draft", "Issued", ... };
and in the model constructor
StatusList = new SelectList(Discountdata),
You're binding Status property to the dropdown. So set the value either from database, or in your case, a default value "Issued" like this:
statusObj.Status = "Issued"
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
I am using MVC 5.
I have my ViewBag for list as
ViewBag.TitleList = new SelectList((new string[] { "Mr", "Miss", "Ms", "Mrs" }), contact.Title);
//contact.Title has selected value.
then I tried converting the array to SelectListItem ( to no avail)
On the View it looks like
#Html.DropDownListFor(model => model.Title, ViewBag.TitleList as SelectList, "Select")
also I tried
#Html.DropDownList("Title", ViewBag.TitleList as SelectList, "Select")
The list loads successfully but the Selected Value is Not selected.
How to Fix this problem?
Update
The culprit was ViewBag.Title matching my model.Title. Renamed my model property to something else and it worked. Arrgh!
Set value of the Title property in the controller:
ViewBag.TitleList = new SelectList(new string[] { "Mr", "Miss", "Ms", "Mrs" });
viewModel.Title = "Miss"; // Miss will be selected by default
Another possible reason (and the correct one, based on the comments below) is that ViewData["Title"] is overridden by another value. Change name of the Title property to any other and everything should work.
When a value is not specified (i.e. "Id"), DropDownListFor sometimes does not behave properly. Try this:
public class FooModel
{
public int Id { get; set; }
public string Text { get; set; }
}
var temp = new FooModel[]
{
new FooModel {Id = 1, Text = "Mr"}, new FooModel {Id = 2, Text = "Miss"},
new FooModel {Id = 3, Text = "Ms"}, new FooModel {Id = 4, Text = "Mrs"}
};
ViewBag.TitleList = new SelectList(temp, "Id", "Text", 2);
EDIT: other sololution
var temp = new []
{
new SelectListItem {Value = "Mr", Text = "Mr"}, new SelectListItem {Value = "Miss", Text = "Miss"},
new SelectListItem {Value = "Ms", Text = "Ms"}, new SelectListItem {Value = "Mrs", Text = "Mrs"}
};
ViewBag.TitleList = new SelectList(temp, "Value", "Text", "Miss");
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 --")
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();" })