我们如何在View .cshtml页面中使用@ Html.RadioButton?

时间:2015-02-20 17:11:01

标签: c# jquery asp.net asp.net-mvc asp.net-mvc-4

我已成功将Contoso University ASP.NET MVC项目集成到我的VS解决方案中。我想添加某种事件处理功能,我可以在索引页面上选择一个或多个项目,例如,点击按钮并根据用户选择从数据库中检索某种数据。目前,我想知道在点击页面上的按钮后如何检索用户选择的项目。在我们甚至与数据库交互之前。

如何为每个项目添加正确的单选按钮,以便正确捕获所选项目?

目前我已为每件商品添加了@Html.RadioButton()。但是,我不知道该把什么作为论据?

如何添加按钮来检索所选数据? 在哪里,以及如何将我的事件处理逻辑用于检索已选择的项目?

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

...

1 个答案:

答案 0 :(得分:2)

如果您的课程实体中有属性,例如courseradio,

 @Html.RadioButtonFor(modelitem => item.courseradio,<value you want to send to controller>)

作为一个例子 如果你想使用单选按钮发送男性或女性

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

}