Get selected values from multiple selectlists in MVC3 controller
Asked Answered
H

1

6

There is a view displaying 5 dropdown lists populated with all available courses from relevant table:

@model StudentRegistrationPortal.Models.CourseRegisterModel
@{
    ViewBag.Title = "registerCourses";
}

<h2>Welcome 
@Context.User.Identity.Name
</h2>
@Html.ActionLink("[Sign Out]", "SignOut", "Admin")

@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
    <legend>Following are available Courses - Please select Courses to  Register</legend>
    <table>
        <tr>
            <td>
                <div class="editor-label">
                    Course-1: 
                </div>
            </td>
            <td>
                <div class="editor-field">
                    @Html.DropDownListFor(m => m.Course.CId, Model.CoursesList)
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <div class="editor-label">
                    Course-2: 
                </div>
            </td>
            <td>
                <div class="editor-field">
                    @Html.DropDownListFor(m => m.Course.CId, Model.CoursesList)
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <div class="editor-label">
                    Course-3: 
                </div>
            </td>
            <td>
                <div class="editor-field">
                    @Html.DropDownListFor(m => m.Course.CId, Model.CoursesList)
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <div class="editor-label">
                    Course-4: 
                </div>
            </td>
            <td>
                <div class="editor-field">
                    @Html.DropDownListFor(m => m.Course.CId, Model.CoursesList)
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <div class="editor-label">
                    Course-5: 
                </div>
            </td>
            <td>
                <div class="editor-field">
                    @Html.DropDownListFor(m => m.Course.CId, Model.CoursesList)
                </div>
            </td>
        </tr>
    </table>


    <p>
        <input type="submit" value="Register" />
    </p>
</fieldset>
}
<div>
    @Html.ActionLink("Back to List", "Home","Student")
</div>

Student will select one course from each dropdown lists and press Register button.

My question is how I will get selected courses in relevant controller?

Thanks.

Homogony answered 29/7, 2013 at 12:0 Comment(3)
can you please tell me why you have Bind with the same value m.Course.CId in each dropdown?Armorial
Because I need to show all courses in each dropdown list...Homogony
Yes, But have binded the same value for each dropdown. I suppose it should not be the caseArmorial
A
3

What you should really do is in your model have properties SelectedCourse1, SelectedCourse2 etc., populate them accordingly and send the model back to the controller

Allegorical answered 29/7, 2013 at 12:9 Comment(1)
Thanks, I introduced 5 course variables in CourseRegisterModel class and get their values on POST request.Homogony

© 2022 - 2024 — McMap. All rights reserved.