var EmpPreviousChart = (from m in dataContext.ViewName
where ((m.EmployeeID == Id)&&(m.Status=='C'))
orderby m.EmployeeID descending
select m.EmployeeID + "- " + m.JoinDate.ToString());
ViewData["EmpPreviousChartList"] = EmpPreviousChart;
The above code I am using to retrieve data using linq.
<%= Html.DropDownList("EmpPreviousCharts", new SelectList((IEnumerable)ViewData["EmpPreviousChartList"]), "Select", new { style = "width:60px;font-family:Verdana;font-size:9px;", ID = "EmpPrevCharts" })%>
the above code to fill my dropdown, I m getting select in very first place. Now I want that the value that should be visible after loading the dropdown should be next to select.
ie If I had select,A,B,C resp. I want A to be visible. I m using partialview for this.
If I could add an autoincrement field in option value I can achive it
or if there is any other way out
SelectList takes the currently selected item as one of the parameters - just pass the item you want to be selected.
You should be instantiating SelectList in your controller, and then pass it to the view. The view should have as little logic as possible.
var EmpPreviousChart = (from m in dataContext.ViewName
where ((m.EmployeeID == Id)&&(m.Status=='C'))
orderby m.EmployeeID descending
select m.EmployeeID + "- " + m.JoinDate.ToString()).ToList();
ViewData["EmpPreviousChartList"] = new SelectList(EmpPreviousChart, EmpPreviousChart.FirstOrDefault());
<%= Html.DropDownList("EmpPreviousCharts", (SelectList)ViewData["EmpPreviousChartList"], "Select", new { style = "width:60px;font-family:Verdana;font-size:9px;", ID = "EmpPrevCharts" })%>
Related
I'm experiencing a problem passing a list from a controller to a view. Doing it this way works fine but I don't want to be passing the entire table.
What I want to do is pass just the results of the studies query:
ViewBag.studyList = new SelectList(_context.Studies.OrderBy(p => p.Name), "Id", "Name");
but if I put studies instead of _context.Studies like this:
var studies = (from t in _context.Studies
where !((from s in _context.Studies
join sn in _context.StudyNodes on s.Id equals sn.StudyId
where sn.NodeId == id
select s.Id).ToList()).Contains(t.Id)
select new BundleNodeViewModel
{
StudyId = t.Id,
StudyName = t.Name
}).ToList();
ViewBag.studyList = new SelectList(studies.OrderBy(p => p.StudyName), "Id", "Name");
The code throws a null exception error in the view.
How can this be a problem? I need to pass the filtered results as per the studies query and not the entire list which is the only way it seems to work.
Can someone help please so I can pass studies and not _context.Studies.
Thanks for your help.
So your result from studies is list of BundleNodeViewMode and you want to list the item from there. Then you should tell the selectlist what is the Id and Name you are displaying from.
ViewBag.studyList = new SelectList((from s in studies.OrderBy(p => p.StudyName), select new {Id = s.StudyId, Name = s.StudyName}) , "Id", "Name");
I have the following code where I am selecting 100 items from the db, but would like to display only 5 in the dropdown. The .Take method does not seem to work.
HeatService heatService = new HeatService();
List<HeatDropdownOption> availableHeats = heatService.GetHeats()
.Where(h => h.ComponentType.Equals(componentType.ToString()))
.OrderByDescending(h => h.Date)
.Take(Constants.NMostRecentHeats)
.Select(h => new HeatDropdownOption(h))
.ToList();
//HeatDropdown = new MultiSelectList(availableHeats, "ID", "Label", selectedHeats.Select(h=>h.ID).ToList());
HeatDropdown = new MultiSelectList(availableHeats, "ID", "Label", selectedHeats.Take(5).Select(h => h.ID).ToList());
if you look at the overloads of MultiSelectList the 4th overload is to select items in the list and the 1st overload is to set the data to dropdown so we can use .Take(5) to filter the data before populating it in the dropdown. so if you want to display only 5 in dropdown use take like this
HeatDropdown = new MultiSelectList(availableHeats.Take(5), "ID", "Label");
it will display the first 5 items from the availableHeats
MVC 3 VB.NET razor. I have a view that has 4 dropdown boxes in it.. This is for setting up a staff member.. If that staff member is to work certain classes then that class will be set for each day. If he is not then the value needs to stay null. This is a edit view so it may have to be accessed multiple times and still keep the original selectlist values if none changed. The below is what I have right now that is working only on its face. The Old selected value is shown first. However this isnt being returned on the save... The only way that it will save correctly is if I select the value that was set in each box then click save. The next problem is that not all staff members will have classes to work on every one of the 4 days. So how do I set a value to null and keep it that way unless a class Is actually selected..
Dim _staff As confstaff = db.confstaffs.Single(Function(a) a.id = id)
ViewBag.role = _staff.Conf_Role.ToString
ViewBag.confRole = db.conf_roles.ToList
ViewData("tue_Class") = New SelectList(db.courses.ToList.Where(Function(r) r.course_day = "Tuesday").Select(Function(r) r.course_ref), New With {.value = _staff.tue_class})
ViewData("wed_Class") = New SelectList(db.courses.ToList.Where(Function(r) r.course_day = "Wednesday").Select(Function(r) r.course_ref), New With {.value = _staff.wed_class})
ViewData("thur_Class") = New SelectList(db.courses.ToList.Where(Function(r) r.course_day = "Thursday").Select(Function(r) r.course_ref), New With {.value = _staff.thur_class})
ViewData("fri_Class") = New SelectList(db.courses.ToList.Where(Function(r) r.course_day = "Friday").Select(Function(r) r.course_ref), New With {.value = _staff.fri_class})
Return View(_staff)
And the view is:
<label>Tuesday Class</label>
#Html.DropDownList("tue_class", "Select One")
<label class="small_spacing">Wednesday Class</label>
#Html.DropDownList("wed_class", "Select One")
<label class="small_spacing">Thursday Class</label>
#Html.DropDownList("thur_class", "Select One")
<label class="small_spacing">Friday Class</label>
#Html.DropDownList("fri_class", "Select One")
I already expect someone to point out that I should use a view model instead of viewbag but I dont see how a view model would be practical with there being over 100 different courses but I am open for ideas...
Any Ideas???????
if you look at your page source after the page is loaded you will see your top item set from your viewbag value has no value only text so when it is submitted it thinks you are submitting blank. I ran into this before.
What you need to do is to manually create each of your dropdown lists by iterating through the collection and setting the one that matches your viewbag item to selected, that way you can be sure a selected item has a selected value. I have some Razr code around here somewhere. Will update when I find it.
EDIT
<select name="Type1" id="Type1">
<option value=""></option>
#foreach (var name in ViewBag.BackOfficeTypes)
{
if (name == ViewBag.SelectedType1Value)
{
<option value="#name" selected="selected">#name</option>
}
else
{
<option value="#name">#name</option>
}
}
</select>
I have several of these on the same page building from different sets of items. Hope this helps.
EDIT 2
If you want to do it all from codebehind I am not a VB guy but here is a way to do it but you need to change your linq statement to manually create the list items instead of dumping from the toList Method.
var courses = db.getCourses();
IEnumerable<SelectListItem> selectList =
from c in courses
where c.course_day = "Tuesday"
select new SelectListItem
{
Selected = (c.CourseID == selectedCourseID),
Text = c.Name,
Value = c.CourseID.ToString()
};
If you can translate this into the VB equivalent it might solve your issue instead of building them in the Razor end.
My problem is pretty simple. Lets say I have a dropdown with users.
in the database i have 3 fields for my user table:
user_id
user_name
user_firstname
in my MVC app i want to link those users to projects. so thats why i want the dropdown.
now, i want to have a selectlist, with the ID as the value, and the firstname AND lastname to be the 'text'
SelectList sl = new SelectList(users, "user_id", "user_name");
now how do i get the first name also in the text? this should be fairly easy, but seems it isnt...
Use LINQ to transform your list of users into a list of SelectListItem.
var selectOptions =
from u in yourUserQuery
select new SelectListItem {
Value = u.user_id,
Text = u.user_firstname + " " + u. user_name
};
You can then convert that to a SelectList however you see fit. I personally like to define a .ToSelectList() extension method for IEnumerable<SelectListItem>, but to each his own.
You need to build a list from your database in the format you need. Here is what I did after my database read into a DataTable,
IList<UsrInfo> MyResultList = new List<UsrInfo>();
foreach (DataRow mydataRow in myDataTable.Rows)
{
MyResultList.Add(new UsrInfo()
{
Usr_CD = mydataRow["USR_NR"].ToString().Trim(),
Usr_NA = mydataRow["USR_NA_LAST"].ToString().Trim() + " , " +
mydataRow["USR_NA_FIRST"].ToString().Trim()
});
}
return new SelectList(MyResultList, "Usr_CD", "Usr_NA");
I have a problem with a selectlist, I have 8 items and 3 of them gets the value selected = true in debug, but the rendered Html the item isnt selected.
What might be wrong?
List<SelectListItem> UsergroupID = (from usg in _ug.GetUsergroups().ToList()
join ug in u.Usergroups
on usg.UsergroupID equals ug.UsergroupID into j
select
new SelectListItem
{
Selected = j.Any(),
Value = usg.UsergroupID.ToString(),
Text = usg.UsergroupName
}).ToList();
ViewData["UsergroupID"] = UsergroupID;
return View("UserEdit", new UserAdminEditViewModel { User = u, Usergroups = _ug.GetUsergroups() });
And in my view I have:
<%= Html.ListBox("UsergroupID", (IEnumerable<SelectListItem>)ViewData["UsergroupID"]) %>
What's the reason for it not making the 3 items that have selected = true selected in the selectlist?
/M
looks like another bug in MVC to me ... you can refer to this link explaining part of the issue Link