I'm having trouble to get the value from a selected item dropdownList. I need to pass the value to another view.
Viewmodel:
public class HomeVM
{
public IEnumerable<SelectListItem> UserList { get; set; }
public IEnumerable<Examination> Exam { get; set; }
public string SelectedUser { get; set; }
}
Controller: (here am first checking if there are any active examinations, it works fine.)
public ActionResult Index()
{
var activeExam = db.Examinations.Where(o => (o.StartTime <= DateTime.Now && o.Duration > DateTime.Now)).ToList();
if(activeExam != null)
{
var hVM = new HomeVM { Exam = activeExam };
hVM.UserList = db.Users
.Select(o => new SelectListItem
{
Value = o.UserID.ToString(),
Text = o.Name
});
return View(hVM);
}
var nullHomeView = new HomeVM();
return View(nullHomeView);
}
View: (I've erased the code I thought was just taking space for readability, I've erased code for creating table and the rows etc)
@model ProjWebb.ViewModels.HomeVM
....
@if (Model.Exam != null)
{
foreach (var item in Model.Exam)
{
<tr>
<td width="20%">
@Html.DisplayFor(model => item.Cours.Name)
</td>
<td width="20%">
@Html.DisplayFor(model => item.ExamID)
</td>
<td width="20%">
@Html.DisplayFor(model => item.StartTime)
</td>
<td width="20%">
@Html.DropDownList("UserList")
</td>
I dont know how to get the selected value, especially when not having a BeginForm
.
The plan is to just have a link the user can click on when having selected the name in the dropdownlist, then pass the userID in that link to the controller.