MVC3 SelectedValue - asp.net-mvc

In my model:
public SelectList QuestionGroupSelectList { get; set; }
------
List<QuestionGroup> questionGroupList = questionGroupRepository.GetQuestionGroup_BySurveyId(survey.Id);
Dictionary<int, string> questionGroupDictionary = questionGroupList.ToDictionary(l => l.Id, l => l.Name);
QuestionGroupSelectList = new SelectList(questionGroupDictionary, "key", "value", questionGroupId);
---------------------------------------
In view:
#Html.DropDownList("QuestionGroupSelectList", Model.QuestionGroupSelectList, "Choose Here")
When i debug i get 2 items in QuestionGroupSelectList (one with Id 30 and one with Id 35), and it says that the selectedValue is 35 (questionGroupId = 35)
But the selectedvalue does not work in the view, any ideas?
Thanks in advance!

You should use a different property to bind your dropdownlist value to. Also you should use view models and strongly typed helpers, like this:
public class MyViewModel
{
public int QuestionGroupId { get; set; }
public SelectList QuestionGroupSelectList { get; set; }
}
then you could have a controller action which populates this view model and passes it to the view:
public ActionResult Foo()
{
// This collection could come from anywhere
// normally you will query a repository here to fetch those values
var values = new[]
{
new { Key = "1", Value = "item 1" },
new { Key = "2", Value = "item 2" },
new { Key = "3", Value = "item 3" },
}
var model = new MyViewModel
{
// preselect the second value
QuestionGroupId = 2,
QuestionGroupSelectList = new SelectList(values, "Key", "Value")
}
return View(model);
}
and finally in your view:
#model MyViewModel
#Html.DropDownListFor(
x => x.QuestionGroupId,
Model.QuestionGroupSelectList,
"Choose Here"
)

Related

Drop down list value returns to ---select--- after page refresh

So in my application the user will select a name from the drop down list, click 'view' and the corresponding values will display on page.
A hyperlink is then used to sort the list in ascending order. For this to happen the page refreshes and displays the new order of the list.
The value of the drop down list returns back to its original value of 'select' instead of remaining the name of the person selected.
My Model:
public class HolidayList
{
public List<Holiday> HList4DD { get; set; }
public List<Person> PList4DD { get; set; }
public int currentPersonID { get; set; }
public IEnumerable<SelectListItem> Categories { get; set; }
public HolidayList()
{
HList4DD = new List<Holiday>();
PList4DD = new List<Person>();
}
}
}
my controller:
[HttpPost]
public ViewResult Index(int HolidayDate)
{
var holidays = db.Holidays.Include("Person");
HolidayList model = new HolidayList();
model.currentPersonID = HolidayDate;
model.PList4DD = db.People.ToList();
model.Categories = holidays.Select(x => new SelectListItem
{
Value = x.Id.ToString(),
Text = x.Person.Name
}
);
int data = HolidayDate;
model.HList4DD = db.Holidays.Where(h => h.PersonId == HolidayDate).ToList();
return View(model);
}
[HttpGet]
public ViewResult Index(string sortOrder, int? currentPersonID)
{
var holidays = db.Holidays.Include("Person");
HolidayList model = new HolidayList();
//not null
if (currentPersonID.HasValue)
{
model.currentPersonID = currentPersonID.Value;
}
else
{
model.currentPersonID = 0;
}
model.PList4DD = db.People.ToList();
ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "date" : "";
var dates = from d in db.Holidays
where d.PersonId == currentPersonID.Value
select d;
switch (sortOrder)
{
case "date":
dates = dates.OrderBy(p => p.HolidayDate);
break;
}
model.HList4DD = dates.ToList();
return View(model);
}
my view
i've tried a number of different attempts here, the following code worked but has the drop list problem
#Html.DropDownListFor(model => model.HList4DD.First().HolidayDate,
new SelectList(Model.PList4DD, "Id", "Name"),
// Model.currentPersonID
"---Select---"
) *#
my attempts to resolve this are:
#Html.DropDownList("HolidayDate", Model.Categories, "---Select---")
#Html.DropDownListFor("HolidayDate", x => x.HolidayDate, Model.Categories)
Any help much appreciated
You are binding the DropDownFor to a wrong property.
Basically what you want to do is in your Model, create a new Property to bind the value selected by the dropdown.
public int SelectedDate {get;set;}
Then in your code front you wanted to use dropdownFor to bind the property like this
#Html.DropDownListFor(model => model.SelectedDate ,
new SelectList(Model.PList4DD, "Id", "Name"),
// Model.currentPersonID
"---Select---"
)
Not this.
#Html.DropDownListFor(model => model.HList4DD.First().HolidayDate ,
new SelectList(Model.PList4DD, "Id", "Name"),
// Model.currentPersonID
"---Select---"
)
Finnaly, in the action that you wanted to do the sorting, you will need to pass the SelectedDate into the action. Then before you returning it, assign it to Model. And the whole thing will work like magic.

MVC DropDownList SelectedValue not displaying correctly

I tried searching and didn't find anything that fixed my problem. I have a DropDownList on a Razor view that will not show the the item that I have marked as Selected in the SelectList. Here is the controller code that populates the list:
var statuses = new SelectList(db.OrderStatuses, "ID", "Name", order.Status.ID.ToString());
ViewBag.Statuses = statuses;
return View(vm);
Here is the View code:
<div class="display-label">
Order Status</div>
<div class="editor-field">
#Html.DropDownListFor(model => model.StatusID, (SelectList)ViewBag.Statuses)
#Html.ValidationMessageFor(model => model.StatusID)
</div>
I walk through it and even in the view it has the correct SelectedValue however the DDL always shows the first item in the list regardless of the selected value. Can anyone point out what I am doing wrong to get the DDL to default to the SelectValue?
The last argument of the SelectList constructor (in which you hope to be able to pass the selected value id) is ignored because the DropDownListFor helper uses the lambda expression you passed as first argument and uses the value of the specific property.
So here's the ugly way to do that:
Model:
public class MyModel
{
public int StatusID { get; set; }
}
Controller:
public class HomeController : Controller
{
public ActionResult Index()
{
// TODO: obviously this comes from your DB,
// but I hate showing code on SO that people are
// not able to compile and play with because it has
// gazzilion of external dependencies
var statuses = new SelectList(
new[]
{
new { ID = 1, Name = "status 1" },
new { ID = 2, Name = "status 2" },
new { ID = 3, Name = "status 3" },
new { ID = 4, Name = "status 4" },
},
"ID",
"Name"
);
ViewBag.Statuses = statuses;
var model = new MyModel();
model.StatusID = 3; // preselect the element with ID=3 in the list
return View(model);
}
}
View:
#model MyModel
...
#Html.DropDownListFor(model => model.StatusID, (SelectList)ViewBag.Statuses)
and here's the correct way, using real view model:
Model
public class MyModel
{
public int StatusID { get; set; }
public IEnumerable<SelectListItem> Statuses { get; set; }
}
Controller:
public class HomeController : Controller
{
public ActionResult Index()
{
// TODO: obviously this comes from your DB,
// but I hate showing code on SO that people are
// not able to compile and play with because it has
// gazzilion of external dependencies
var statuses = new SelectList(
new[]
{
new { ID = 1, Name = "status 1" },
new { ID = 2, Name = "status 2" },
new { ID = 3, Name = "status 3" },
new { ID = 4, Name = "status 4" },
},
"ID",
"Name"
);
var model = new MyModel();
model.Statuses = statuses;
model.StatusID = 3; // preselect the element with ID=3 in the list
return View(model);
}
}
View:
#model MyModel
...
#Html.DropDownListFor(model => model.StatusID, Model.Statuses)
Make Sure that your return Selection Value is a String and not and int when you declare it in your model.
Example:
public class MyModel
{
public string StatusID { get; set; }
}
Create a view model for each view. Doing it this way you will only include what is needed on the screen. As I don't know where you are using this code, let us assume that you have a Create view to add a new order.
Create a new view model for your Create view:
public class OrderCreateViewModel
{
// Include other properties if needed, these are just for demo purposes
// This is the unique identifier of your order status,
// i.e. foreign key in your order table
public int OrderStatusId { get; set; }
// This is a list of all your order statuses populated from your order status table
public IEnumerable<OrderStatus> OrderStatuses { get; set; }
}
Order status class:
public class OrderStatus
{
public int Id { get; set; }
public string Name { get; set; }
}
In your Create view you would have the following:
#model MyProject.ViewModels.OrderCreateViewModel
#using (Html.BeginForm())
{
<table>
<tr>
<td><b>Order Status:</b></td>
<td>
#Html.DropDownListFor(x => x.OrderStatusId,
new SelectList(Model.OrderStatuses, "Id", "Name", Model.OrderStatusId),
"-- Select --"
)
#Html.ValidationMessageFor(x => x.OrderStatusId)
</td>
</tr>
</table>
<!-- Add other HTML controls if required and your submit button -->
}
Your Create action methods:
public ActionResult Create()
{
OrderCreateViewModel viewModel = new OrderCreateViewModel
{
// Here you do database call to populate your dropdown
OrderStatuses = orderStatusService.GetAllOrderStatuses()
};
return View(viewModel);
}
[HttpPost]
public ActionResult Create(OrderCreateViewModel viewModel)
{
// Check that viewModel is not null
if (!ModelState.IsValid)
{
viewModel.OrderStatuses = orderStatusService.GetAllOrderStatuses();
return View(viewModel);
}
// Mapping
// Insert order into database
// Return the view where you need to be
}
This will persist your selections when you click the submit button and is redirected back to the create view for error handling.
I hope this helps.
For me, the issue was caused by big css padding numbers ( top & bottom padding inside the dropdown field). Basically, the item was being shown but not visible because it was way down. I FIXED it by making my padding numbers smaller.
I leave this in case it helps someone else. I had a very similar problem and none of the answers helped.
I had a property in my ViewData with the same name as the selector for the lambda expression, basically as if you would've had ViewData["StatusId"] set to something.
After I changed the name of the anonymous property in the ViewData the DropDownList helper worked as expected.
Weird though.
My solution was this...
Where the current selected item is the ProjectManagerID.
View:
#Html.DropDownList("ProjectManagerID", Model.DropDownListProjectManager, new { #class = "form-control" })
Model:
public class ClsDropDownCollection
{
public List<SelectListItem> DropDownListProjectManager { get; set; }
public Guid ProjectManagerID { get; set; }
}
Generate dropdown:
public List<SelectListItem> ProjectManagerDropdown()
{
List<SelectListItem> dropDown = new List<SelectListItem>();
SelectListItem listItem = new SelectListItem();
List<ClsProjectManager> tempList = bc.GetAllProductManagers();
foreach (ClsProjectManager item in tempList)
{
listItem = new SelectListItem();
listItem.Text = item.ProjectManagerName;
listItem.Value = item.ProjectManagerID.ToString();
dropDown.Add(listItem);
}
return dropDown;
}
Please find sample code below.
public class Temp
{
public int id { get; set; }
public string valueString { get; set; }
}
Controller
public ActionResult Index()
{
// Assuming here that you have written a method which will return the list of Temp objects.
List<Temp> temps = GetList();
var tempData = new SelectList(temps, "id", "valueString",3);
ViewBag.Statuses = tempData;
return View();
}
View
#Html.DropDownListFor(model => model.id, (SelectList)ViewBag.Statuses)
#Html.ValidationMessageFor(model => model.id)

Html.Dropdonlist and selected value

I have the following code:
int StatusID = ViewBag.DefaultStatus.StatusID;
List<NEOGOV_Ideas.Models.Status> statusList = ViewBag.StatusList;
#Html.DropDownList("DefaultStatus", new SelectList(statusList, "StatusID", "StatusName", StatusID))
but selected value does not set. Why?
Try like this in your controller action:
// set to a value that you want to be preselected
ViewBag.DefaultStatus = 123;
ViewBag.StatusList = ...
and in the view:
#{ List<NEOGOV_Ideas.Models.Status> statusList = ViewBag.StatusList; }
#Html.DropDownList(
"DefaultStatus",
new SelectList(statusList, "StatusID", "StatusName")
)
But I would recommend you using view models:
public class MyViewModel
{
public int SelectedStatusID { get; set; }
public SelectList Statuses { get; set; }
}
then in your controller action populate this view model and pass it to the view:
public ActionResult Foo()
{
List<NEOGOV_Ideas.Models.Status> statusList = ...
var model = new MyViewModel
{
// set to a value that you want to be preselected
SelectedStatusID = 123,
Statuses = new SelectList(statusList, "StatusID", "StatusName")
};
return View(model);
}
and finally in your strongly typed view:
#model MyViewModel
#Html.DropDownListFor(x => x.SelectedStatusID, Model.Statuses)

ASP.net MVC Listbox

I have a number of Html.Listbox controls on my view which are being populated by an IEnumberable<SelectListItem>
Is there a way that when the data is populated, that some items are automatically selected?
The particular selectlist items in the IEnumerable are marked as Selected = true, yet this does not convey into the view.
The view is as such:
<%= Html.ListBox("projects", Model.Projects)%>
Thanks.
Sure as always you start by defining a view model that will represent the information you would like to show on the particular view:
public class MyViewModel
{
public string[] SelectedItems { get; set; }
public IEnumerable<SelectListItem> Items { get; set; }
}
then a controller:
public class HomeController : Controller
{
public ActionResult Index()
{
var model = new MyViewModel
{
SelectedItems = new[] { "1", "3" },
Items = new[]
{
new SelectListItem { Value = "1", Text = "Item 1" },
new SelectListItem { Value = "2", Text = "Item 2" },
new SelectListItem { Value = "3", Text = "Item 3" },
}
};
return View(model);
}
}
and finally in your strongly typed view:
<%= Html.ListBoxFor(
x => x.SelectedItems,
new SelectList(Model.Items, "Value", "Text")
) %>
As expected Item 1 and Item 3 will be preselected when the list box is rendered.

Having difficulty using an ASP.NET MVC ViewBag and DropDownListfor

My difficulty is how to use ViewBag with DropdownListFor?
In my controller I am having:
TestModel model = new TestModel();
ViewBag.Clients = model.Clients;
ViewBag.StatusList = model.StatusList;
ViewBag.enumStatus = model.enumStatus;
ViewBag.intClient = model.intClient;
In my TestModel
public SelectList Clients { get; set; }
public SelectList StatusList { get; set; }
public ActiveStatus enumStatus { get; set; }
public int? intClient { get; set; }
In my View
I want to use DropDownListFor to display the ViewBag values, how can I do that?
You could do this:
#Html.DropDownListFor(x => x.intClient, ViewBag.Clients)
But I would recommend you to avoid ViewBag/ViewData and profit from your view model:
public ActionResult Index()
{
var model = new TestModel();
model.Clients = new SelectList(new[]
{
new { Value = "1", Text = "client 1" },
new { Value = "2", Text = "client 2" },
new { Value = "3", Text = "client 3" },
}, "Value", "Text");
model.intClient = 2;
return View(model);
}
and in the view:
#Html.DropDownListFor(x => x.intClient, Model.Clients)
Personally...I create a List and do this.
public ActionResult SomeAction()
{
var list = new List<SelectListItem>();
list.Add(new SelectListItem(){Text = "One", Value="One"});
list.Add(new SelectListItem(){Text = "Two", Value="Two"});
list.Add(new SelectListItem(){Text = "Three", Value="Three"});
list.Add(new SelectListItem(){Text = "Four", Value="Four"});
ViewBag.Clients = list;
return View();
}
and then in your view...
#Html.DropDownListFor(x => x.SomePropertyOnModel, (IEnumerable<SelectListItem>)ViewBag.Clients);
Notice the cast on the Viewbag item. The cast is required because the viewbag has no idea what the object is for Viewbag.Client. So the cast there is required.
#Html.DropDownListFor(x => x.intClient, new SelectList(Model.Clients, "ClientId", "ClientName"), string.Empty);
The ClientId is the value of the dropdown option.
The ClientName is the text of the dropdown option.
The string.Empty at the end adds a blank entry to the dropdown.
Here you can find a good reference: http://blogs.msdn.com/b/nunos/archive/2010/02/08/quick-tips-about-asp-net-mvc-editor-templates.aspx

Resources