MVC5 DropDownListFor not selecting correct value - asp.net-mvc

I realize there are tons of questions on SO about this particular issue, however none of the answers that I've already found are doing quite what I am doing.
View Model
public FreightDiscountViewModel()
{
Sign = new List<SelectListItem>();
States = new List<SelectListItem>();
FreightDiscounts = new List<FreightDiscountModel>();
PopSign();
PopStates();
}
public List<FreightDiscountModel> FreightDiscounts { get; set; }
public List<SelectListItem> States { get; set; }
public List<SelectListItem> Sign { get; set; }
private void PopSign ()
{
Sign.Add(new SelectListItem { Text = "-", Value = "-" });
Sign.Add(new SelectListItem { Text = "+", Value = "+" });
}
private void PopStates ()
{
States.Add(new SelectListItem { Value = "AL", Text = "Alabama" });
States.Add(new SelectListItem { Value = "AK", Text = "Alaska" });
States.Add(new SelectListItem { Value = "AZ", Text = "Arizona" });
States.Add(new SelectListItem { Value = "AR", Text = "Arkansas" });
States.Add(new SelectListItem { Value = "CA", Text = "California" });
States.Add(new SelectListItem { Value = "CO", Text = "Colorado" });
}
}
View
#for (var i = 0; i < Model.FreightDiscounts.Count; i++ )
{
<tr>
<td>#Html.DropDownListFor(x => x.FreightDiscounts[i].State, Model.States, new { #class = "form-control" })</td>
</tr>
}
I am populating my FreightDiscounts list in my view model without issue, and right now for testing, I only have 1 state being returned, Alaska. So the 1 record that being populated in that list has the following info
AK,
US,
50,
0,
+
My question is that when the view loads, the state dropdown for the 1 record is set to Alabama (AL), and not Alaska like I would expect. Does anyone see anything obvious I am missing?\
Edit
JamieD77's answer fixed my problem. I changed my View to the following.
<td>
#Html.DropDownListFor(x => x.FreightDiscounts[i].State,
new SelectList(Model.DStates, "key", "value", Model.FreightDiscounts[i].State), new { #class = "form-control" })
</td>
And I changed my View Model to the following
public Dictionary<String, String> DStates { get; set; }
DStates.Add("AL","Alabama" );
DStates.Add("AK","Alaska" );

try using a SelectList and setting the selected item when you build the dropdownlistfor
#Html.DropDownListFor(x => x.FreightDiscounts[i].State,
new SelectList(Model.States, "Value", "Text", x.FreightDiscounts[i].State),
new { #class = "form-control" })

Possibly because your Text and Value fields are reversed?
Edit: OP has updated his code, they originally were reversed.

Related

dropdownlistfor selected value with viewdata

I am not able to bind the selected value in MVC. Can someone tell me what is wrong with the following code:
#Html.DropDownListFor(x => Model.Members[i].OccupationCd,
(IEnumerable<SelectListItem>)ViewData["ddl_occupation"],
new { #style = "width:100px", #class = "Occupation required" })
public List<SelectListItem> GetOccupation(string selectedValue)
{
List<SelectListItem> selLstOccupation = new List<SelectListItem>();
selLstOccupation.Add(new SelectListItem { Value = "", Text = "---" + ("Select Occupation") + "---" });
selLstOccupation.AddRange(GetData.AllOccupation());
selLstOccupation = GetData.GetSelectedList(selLstOccupation, selectedValue);
return selLstOccupation;
}
public class Member()
{
//code
//code
public int educationCd { get; set; }
}
I found the Solution:
#Html.DropDownListFor(x => Model.Members[i].OccupationCd,new SelectList((IEnumerable<SelectListItem>)ViewData["ddl_occupation"],"Value","Text",Model.Members[i].OccupationCd))
You have to do two things to fix your problem. The first one is to change the GetOccupation method with the following implementation
public List<Occupation> GetOccupation()
{
return GetData.AllOccupation();
}
Then you have to change the dropdown initialization to the following
#Html.DropDownListFor(x => x.Members[i].OccupationCd,
new SelectList(
(IEnumerable<Occupation>)ViewData["ddl_occupation"],
"OccupationCd",
"##HERE YOU ADD THE PROPERTY YOU WANT TO VISUALIZE##",
Model.Members[i].OccupationCd),
"---Select Occupation--",
new { #style = "width:100px", #class = "Occupation required" })
This should fix your problem.

#Html.Dropdownlist(x => x.id, new int) not working

How i can make this work
#using (Html.BeginForm())
{
#Html.ValidationSummary()
<p>MYep #Html.DropDownListFor(x => x.Id, new int {
new SelectListItem() {Text = "asdad", Value = "1"},
new SelectListItem() {Text = "dfgdgd", Value = "2"},
new SelectListItem() {Text = "werwrwt", Value = "3"}, "Choose")
<input type="submit" value="Ok" />
}
Or should I make a IEnumerable list before and give it to dropdownlistfor?
Matt's answer is fine, but it looks like you are using a ViewModel, so you should just put this static list into the view model and reference the list in your View.
ViewModel:
public sealed class MyViewModel
{
public int Id { get; set; }
public List<SelectListItem> IdList
{
get
{
return new List<SelectListItem>
{
new SelectListItem { Text = "asdad", Value = "1" },
new SelectListItem { Text = "dfgdgd", Value = "2" },
new SelectListItem { Text = "werwrwt", Value = "3" }
};
}
}
}
View:
#Html.DropDownListFor(x => x.Id, Model.IdList)
You are declaring an object of type int and populating it with a List of SelectListItems, which will not work.
I think you want something like this... You may have to make some modifications if x.Id is an int and all the dropdown values are strings, but I'm not sure.
#Html.DropDownListFor(x => x.Id, new List<SelectListItem> {
new SelectListItem() {Text = "asdad", Value = "1"},
new SelectListItem() {Text = "dfgdgd", Value = "2"},
new SelectListItem() {Text = "werwrwt", Value = "3"}}, "Choose")

The selected item does not display when I use DropDownListFor

I am using the following to generate a drop down list:
#for (var index = 0; index < Model.AdminSummaries.Count(); index++)
{
<div class="rep_tr0">
<div class="rep_td0">
#Html.DropDownListFor(x => Model.AdminSummaries[index].Status,
AdminStatusReference.GetAdminStatusOptions(),
new { id = string.Format("Status_{0}",index ) })
</div>
</div>
}
Here's the HTML it generates:
<select id="Status_1" name="AdminSummaries[1].Status"><option value="1">Released</option>
<option value="2">Review</option>
<option value="3">New</option>
</select>
Here's the class that gives the status options.
public static class AdminStatusReference
{
public static IEnumerable<SelectListItem> GetAdminStatusOptions()
{
return new[]
{
new SelectListItem { Value = "1", Text = "Released" },
new SelectListItem { Value = "2", Text = "Review" },
new SelectListItem { Value = "3", Text = "New" }
};
}
}
Everything works good EXCEPT it doesn't select the items correctly. There's no option with 'selected' to match the data in the AdminSummaries.
How can I make it so the correct select list items are selected?
Just to clarify this. My problem is that if there is a data record with a value of 3 for the status then when I look at the screen I see a select list with the word "Release" showing.
What I need is for the select list to show text that corresponds with the data value.
Here is the more accurate answer
public static class AdminStatusReference
{
public static IEnumerable<SelectListItem> GetAdminStatusOptionsFor(AdminSummaries arg)
{
var options = new[]
{
new SelectListItem { Value = "1", Text = "Released" },
new SelectListItem { Value = "2", Text = "Review" },
new SelectListItem { Value = "3", Text = "New" }
};
options.First(o=> o.Value == arg).Selected = true;
return options;
}
}
Set the SelectListItem.Selected property to true:
public static class AdminStatusReference
{
public static IEnumerable<SelectListItem> GetAdminStatusOptions()
{
return new[]
{
new SelectListItem { Value = "1", Text = "Released", Selected = true },
new SelectListItem { Value = "2", Text = "Review" },
new SelectListItem { Value = "3", Text = "New" }
};
}
}
It seams from the source code that the DropDownListFor method (actually the ViewDataEvaluator.Eval method) doesn't support expressions containing indexers. Because your expression: AdminSummaries[index].Status contains an indexer that's why the framework doesn't use the selected value from your model class.
The only solution is to specify the selected item when setting the SelectListItem collection, you can do this by passing the currently selected value to your GetAdminStatusOptions method:
View:
#Html.DropDownListFor(x => Model.AdminSummaries[index].Status,
AdminStatusReference.GetAdminStatusOptions(Model.AdminSummaries[index].Status),
new { id = string.Format("Status_{0}",index ) })
A sample GetAdminStatusOptions implementation:
public static IEnumerable<SelectListItem> GetAdminStatusOptions(string selected = null)
{
var options = new[]
{
new SelectListItem {Value = "1", Text = "Released"},
new SelectListItem {Value = "2", Text = "Review"},
new SelectListItem {Value = "3", Text = "New"}
};
foreach (var option in options)
{
option.Selected = option.Value == selected;
}
return options;
}

How to show multiple selected with asp.net mvc 3 and ListBoxFor?

I have this VM properties
public IList<Guid> SelectedEligiableCategories { get; set; }
public IList<SelectListItem> EligiableCategories { get; set; }
I have this helpers in my view
#Html.LabelFor(x => x.EligibleCategoryFrmVm.SelectedEligiableCategories, "Eligible Categories:")
#Html.ListBoxFor(x => Model.EligibleCategoryFrmVm.SelectedEligiableCategories, Model.EligibleCategoryFrmVm.EligiableCategories, new { #class = "eligibleCategoryListBox" })
I have this code in my controller
List<SelectListItem> eligibleCategoriesListItems = Mapper.Map<List<EligibleCategory>, List<SelectListItem>>(eligibleCategories);
foreach (var rewardTier in creditCard.RewardTiers)
{
CbRewardTierFrmVm rewardTierFrmVm = new CbRewardTierFrmVm();
rewardTierFrmVm.EligibleCategoryFrmVm.EligiableCategories = eligibleCategoriesListItems;
foreach (var ec in rewardTier.EligibleCategories)
{
rewardTierFrmVm.EligibleCategoryFrmVm.SelectedEligiableCategories.Add(ec.Id);
}
vm.CbRewardTierFrmVm.Add(rewardTierFrmVm);
}
Yet when I load up my view. None of values for my ListBox are selected. I am not sure why. If this was a selectList this would work as it would match up the SelectedEligiableCategories to the value in the list.
I am not sure if this is because there is multiple selects
Edit
<select name="CbRewardTierFrmVm[63b504c0-0f9a-47ba-a8ff-db85f48d5f0f].EligibleCategoryFrmVm.SelectedEligiableCategories" multiple="multiple" id="CbRewardTierFrmVm_63b504c0-0f9a-47ba-a8ff-db85f48d5f0f__EligibleCategoryFrmVm_SelectedEligiableCategories" data-val-required="Must choose at least one eligible category." data-val="true" class="eligibleCategoryListBox ui-wizard-content ui-helper-reset ui-state-default" style="display: none;">
<option value="ed2bb5f9-4565-4f69-ab15-9fca011c0692">Gas</option>
</select>
Do you think it is because I am using http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/ ?
Edit2
I gone ahead and make an example. I must be missing something(not sure what). When I use "Darin Dimitrov" it works.
I switched the example to a dropdown as I am getting the same problem with it as well.
In this example I am not using a viewmodel since my initial assumption was somehow the helper I was using from Steven Sanders might be effecting it so I was going off his example.
This does not seem to be the case as I removed it and still get this problem.
public class Gift
{
public string Name { get; set; }
public double Price { get; set; }
public string SelectedItem { get; set; }
public IList<SelectListItem> Items { get; set; }
}
public ActionResult Index()
{
List<SelectListItem> items = new List<SelectListItem>
{
new SelectListItem {Value = "",Text ="--"},
new SelectListItem {Value = "1",Text ="1"},
new SelectListItem {Value = "2",Text ="2"},
};
var initialData = new[] {
new Gift { Name = "Tall Hat", Price = 39.95, Items = items, SelectedItem = "2" },
new Gift { Name = "Long Cloak", Price = 120.00, Items = items, SelectedItem = "1" }
};
return View("Index3",initialData);
}
#model IList<EditorDemo.Models.Gift>
#{
ViewBag.Title = "Index3";
}
#for (int i = 0; i < Model.Count; i++)
{
#Html.DropDownListFor(x => x[i].SelectedItem, new SelectList(Model[i].Items, "Value", "Text"))
}
It seems to not be able to handle when you put it in forloop and try it make more than one dropdown list.
The following works for me.
Model:
public class MyViewModel
{
public IList<Guid> SelectedEligiableCategories { get; set; }
public IList<SelectListItem> EligiableCategories { get; set; }
}
Controller:
public class HomeController : Controller
{
public ActionResult Index()
{
var model = new MyViewModel
{
SelectedEligiableCategories = new[]
{
// preselect the second and the fourth item
new Guid("35830042-3556-11E1-BCDC-A6184924019B"),
new Guid("4253876A-3556-11E1-BC17-B7184924019B")
}.ToList(),
EligiableCategories = new[]
{
new SelectListItem { Value = "2DA62E3A-3556-11E1-8A0A-9B184924019B", Text = "item 1" },
new SelectListItem { Value = "35830042-3556-11E1-BCDC-A6184924019B", Text = "item 2" },
new SelectListItem { Value = "3D07EBAC-3556-11E1-8943-B6184924019B", Text = "item 3" },
new SelectListItem { Value = "4253876A-3556-11E1-BC17-B7184924019B", Text = "item 4" },
}
};
return View(model);
}
}
View:
#model MyViewModel
#using (Html.BeginForm())
{
#Html.ListBoxFor(
x => x.SelectedEligiableCategories,
Model.EligiableCategories,
new { #class = "eligibleCategoryListBox" }
)
}
Result:
UPDATE:
Now that you have shown an example allowing to illustrate the problem, you could specify the selected item when building the SelectList:
#Html.DropDownListFor(
x => x[i].SelectedItem,
new SelectList(Model[i].Items, "Value", "Text", Model[i].SelectedItem)
)
The reason a value was not preselected was because you were binding the dropdownlist to a list of properties (x => x[i].SelectedItem) whereas in my example I was using a simple property.
And if you wanted to do this with the ListBoxFor helper you could use the following:
#Html.ListBoxFor(
x => x[i].SelectedItems,
new MultiSelectList(Model[i].Items, "Value", "Text", Model[i].SelectedItems)
)
The SelectedItems property becomes a collection and we use a MultiSelectList instead of a SelectList.
The main problem is using
#Html.DropDownListFor
instead of this
#Html.ListBoxFor
Using the DropDownListFor will NOT help you with multiple values, whatever you do and no matter what your model is. Once you use ListBoxFor ... it will automatically just work !

DropdownlistFor throws an error

I am using hardcoded string values for dropdownlist to the view , and passing the selected value from database , where 0 = pending , 1 = complete and 3 = awaiting, below is the code for view and controller:
var paymentStatus = new[] { "Pending", "Complete", "AwaitingPayment" };
ViewData["StatusID"] = new SelectList(paymentStatus, "Value", "Text", booking.StatusID);
<tr><td>Status</td><td><%: Html.DropDownListFor(m => m.StatusID, ViewData["StatusID"] as SelectList)%></td></tr>
It comes up with the error :
DataBinding: 'System.String' does not contain a property with the name 'Value'.
The problem with your example is that you are passing a string array into the SelectList and then telling the SelectList to use the Value and Text properties (which a string does not have). You should probably create a class for this:
public class Status {
public int Id { get; set; }
public string Text { get; set; }
}
var statusTypes = new List<Status> {
new Status { Id = 1, Text = "Pending" },
new Status { Id = 2, Text = "Complete" },
new Status { Id = 3, Text = "AwaitingPayment" }
};
Better yet, create a repository for this data:
var statusTypes = statusRepository.GetStatusTypes();
Pass this into your SelectList:
SelectList statusList = new SelectList(statusTypes, "Id", "Text", booking.StatusID);
// return this in a ViewModel or use ViewData like you are now:
ViewData["Status"] = statusList;
return View(statusList);
Please use view models:
var paymentStatuses = new[]
{
new SelectListItem { Value = "0", Text = "Pending" },
new SelectListItem { Value = "1", Text = "Complete" },
new SelectListItem { Value = "3", Text = "AwaitingPayment" },
};
var model = new SomeViewModel
{
StatusID = booking.StatusID,
Statuses = new SelectList(paymentStatuses, "Value", "Text")
}
return View(model);
and then:
<tr>
<td>
Status
</td>
<td>
<%= Html.DropDownListFor(m => m.StatusID, Model.Statuses) %>
</td>
</tr>
or if you insist on this ViewData (I don't recommend it, especially as you already have a view model):
var paymentStatuses = new[]
{
new SelectListItem { Value = "0", Text = "Pending" },
new SelectListItem { Value = "1", Text = "Complete" },
new SelectListItem { Value = "3", Text = "AwaitingPayment" },
};
ViewData["Statuses"] = new SelectList(paymentStatuses, "Value", "Text");
var model = new SomeViewModel
{
StatusID = booking.StatusID
}
return View(model);
and in the view:
<tr>
<td>
Status
</td>
<td>
<%= Html.DropDownListFor(m => m.StatusID, ViewData["Statuses"] as SelectList) %>
</td>
</tr>
The error shows it is unable to Fine "Value" , you have to do something like
new SelectList(paymentStatus, booking.Status, "Text", booking.StatusID)
bookin.Status will be the any text property of booking. hope this help

Resources