I've successfully integrated the Contoso University ASP.NET MVC project in my VS Solution. I'd like to add some sort of event handling feature where I can select one or more items on an index page, for example, hit a button and retrieve some sort of data from the database based on the user selections. For now, I'd like to know how to retrieve the user selected items after I hit a button on the page. Before we even interact with the database.
How do I add the correct radio button for each item, so that the selected items can be captured properly?
For now I've added a @Html.RadioButton()
for each item. However, I don't know what to put as arguments?
How do I add a button to retrieve the selected data?? Where, and what do I put my event handling logic to retrieve what items have been selected?
Views/Course/Index.cshtml
@model IEnumerable<ContosoUniversity.Models.Course>
@{
ViewBag.Title = "Courses";
}
<h2>Courses</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
@using (Html.BeginForm())
{
<p>
Select Department: @Html.DropDownList("SelectedDepartment", "All")
<input type="submit" value="Filter" />
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.CourseID)
</th>
<th>
@Html.DisplayNameFor(model => model.Title)
</th>
<th>
@Html.DisplayNameFor(model => model.Credits)
</th>
<th>
Department
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.RadioButton(courseRadio, )
</td>
<td>
@Html.DisplayFor(modelItem => item.CourseID)
</td>
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
<td>
@Html.DisplayFor(modelItem => item.Credits)
</td>
<td>
@Html.DisplayFor(modelItem => item.Department.Name)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.CourseID }) |
@Html.ActionLink("Details", "Details", new { id = item.CourseID }) |
@Html.ActionLink("Delete", "Delete", new { id = item.CourseID })
</td>
</tr>
}
</table>
}
CourseController.cs:
....
public ActionResult Index(int? SelectedDepartment)
{
var departments = db.Departments.OrderBy(q => q.Name).ToList();
ViewBag.SelectedDepartment = new SelectList(departments, "DepartmentID", "Name", SelectedDepartment);
int departmentID = SelectedDepartment.GetValueOrDefault();
IQueryable<Course> courses = db.Courses
.Where(c => !SelectedDepartment.HasValue || c.DepartmentID == departmentID)
.OrderBy(d => d.CourseID)
.Include(d => d.Department);
var sql = courses.ToString();
return View(courses.ToList());
}
...
if you have an attribute in your course entity like courseradio,
@Html.RadioButtonFor(modelitem => item.courseradio,<value you want to send to controller>)
as an example if you want to send male or female using radio buttons
@model HotelReservationSystem.Models.User
@using (Html.BeginForm("Action","Controller"))
{
@Html.RadioButtonFor(model => model.gender,"Male", new { id = "male" })
@Html.Label("male", "Male")
@Html.RadioButtonFor(model => model.gender,"Female", new { id = "female" })
@Html.Label("female", "Female")
@Html.ValidationMessageFor(model => model.gender, "", new { @class = "red-text" })
<input type="submit" class="btn" value="OK" />
}