-1

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.

JohnDoe
  • 9
  • 2
  • 1
    You cannot use a `foreach` loop for generating form controls for a collection. It needs to be a `for` loop or use a custom `EditorTemplate` - refer [this answer](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943) –  May 25 '16 at 10:21
  • But what property are you wanting to bind the selected value to? –  May 25 '16 at 10:22
  • Probably the HTML-Helper Extention Method [BeginCollectionItem](http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/) could help you – Maximilian Ast May 25 '16 at 10:43
  • i thought i bind it to selectedUser, though i think i will make it an int instead of string, to save the userId, i need to save the user id and pass it to the next view where a user can take the exam, so the user will be saved with the correct exam. – JohnDoe May 25 '16 at 11:15
  • Your view is not making any sense. What are you generating multiple controls to select a user when it appears you only want to send one value to the 'next' view? And you would not even know which exam you selected the user for. And why are you not using a form? –  May 25 '16 at 11:29
  • The application is for students to write exams, so a teacher can create exams for different courses, and set a start/end time for that particular exam, the students can see the view that am currently working on, the one pasted here. There already exist users, so i thought that the student should be able to chose their name in a dropdownlist and then get to the write exam page (view) by actionlink for example. – JohnDoe May 25 '16 at 11:37
  • Surely that would require a user to be logged in so you already know who the student is. –  May 25 '16 at 11:44
  • normally ofc, but am trying to learn so i havent come that far yet, surely there must be a way to get the id from a list of users in a dropdownlist, would you say i need @html.beginform in my view? – JohnDoe May 25 '16 at 11:50
  • Just add a form that posts back to a `[HttPost]` method and add `@Html.DropDownListFor(m => m.SelectedUser, Model.UserList)` and a submit button. If the method has a parameter `int SelectedUser` it will be bound with the selected `UserID` value. –  May 27 '16 at 01:17

1 Answers1

0
hVM.UserList = db.Users
                .Select(o => new SelectListItem
                {
                    Value = o.UserID.ToString(),
                    Text = o.Name
                },selectedUserId.Value);

you can get the value in model and that value you can set here. as selectedUserId.value

Dhruti
  • 37
  • 2
  • 10
  • ah ok, i will try that, i didnt know that was possible, thanks for the input. – JohnDoe May 25 '16 at 11:16
  • @JohnDoe, Its not possible - this code would not even compile. And it has nothing to do with your qustion –  May 25 '16 at 11:45