0

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());
        }

...
jerryh91
  • 1,777
  • 10
  • 46
  • 77
  • maybe you can get answer from here. http://stackoverflow.com/questions/10805502/mvc-razor-radio-button – jkl Feb 20 '15 at 17:15
  • What's the difference in usage and behavior between @Html.RadioButton() and @Html.RadioButtonFor()? – jerryh91 Feb 20 '15 at 17:38
  • `RadioButtonFor()` is bonded with model property, while `RadioButton()` is not – Ehsan Sajjad Feb 20 '15 at 17:47
  • @jerryh91 add a ``bool`` property in your model named Selected and use ``RadioButtonFor()`` – Ehsan Sajjad Feb 20 '15 at 17:53
  • Can you explain what "bonded with model property" means? – jerryh91 Feb 20 '15 at 19:00
  • Radio buttons are for selecting one option from a group of many. I suspect what you want here is a checkbox so that you can select multiple courses, but its not clear what your intending to do with the 'selected' courses –  Feb 20 '15 at 22:27
  • Yes, I'd like to be able to select > 1 course. I'd like to be able to retrieve some sort of data that's not in the current "Course" table from these selected courses. Maybe, all the students who's enrolled in the course? Or all the instructors who's teaching this course. I'd just like to know how to write an event handler for this action button. – jerryh91 Feb 23 '15 at 16:06
  • How do we write the client side code to retrieve the selected courses – jerryh91 Feb 23 '15 at 16:29

2 Answers2

2

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" />

}
Dimuth Ruwantha
  • 671
  • 2
  • 12
  • 26
0

enter image description here

public class ContactModel  
{  
    public string Name { get; set; }  
    public string Gender { get; set; }  
    public string PhoneNumber { get; set; }  
}

<div class="col-md-10">  
    Male  
    @Html.RadioButtonFor(model => model.Gender, "Male")  
    Female  
    @Html.RadioButtonFor(model => model.Gender, "Female")  
</div> 

References: https://www.c-sharpcorner.com/article/radiobutton-in-asp-net-mvc/