Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在View.cshtml页面中使用@Html.RadioButton?_C#_Jquery_Asp.net_Asp.net Mvc_Asp.net Mvc 4 - Fatal编程技术网

C# 如何在View.cshtml页面中使用@Html.RadioButton?

C# 如何在View.cshtml页面中使用@Html.RadioButton?,c#,jquery,asp.net,asp.net-mvc,asp.net-mvc-4,C#,Jquery,Asp.net,Asp.net Mvc,Asp.net Mvc 4,我已经成功地将Contoso University ASP.NET MVC项目集成到我的VS解决方案中。我想添加一些事件处理功能,可以在索引页上选择一个或多个项目,例如,点击按钮并根据用户选择从数据库中检索某种类型的数据。现在,我想知道在点击页面上的按钮后如何检索用户选择的项目。甚至在我们与数据库交互之前 如何为每个项目添加正确的单选按钮,以便正确捕获选定的项目 现在,我为每个项目添加了一个@Html.RadioButton()。然而,我不知道该把什么作为论点 如何添加按钮以检索所选数据?? 在

我已经成功地将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());
        }

...
@model IEnumerable
@{
ViewBag.Title=“课程”;
}
课程

@ActionLink(“新建”、“创建”)

@使用(Html.BeginForm()) { 选择部门:@Html.DropDownList(“SelectedDepartment”、“All”)

@DisplayNameFor(model=>model.CourseID) @DisplayNameFor(model=>model.Title) @DisplayNameFor(model=>model.Credits) 部门 @foreach(模型中的var项目) { @Html.RadioButton(courseRadio,) @DisplayFor(modelItem=>item.CourseID) @DisplayFor(modeleItem=>item.Title) @DisplayFor(modelItem=>item.Credits) @DisplayFor(modelItem=>item.Department.Name) @ActionLink(“编辑”,“编辑”,新的{id=item.CourseID})| @ActionLink(“详细信息”,“详细信息”,新的{id=item.CourseID})| @ActionLink(“删除”,“删除”,新的{id=item.CourseID}) } } CourseController.cs: .... 公共行动结果索引(int?选定部门) { var departments=db.departments.OrderBy(q=>q.Name.ToList(); ViewBag.SelectedDepartment=新的SelectedList(部门,“部门ID”,“名称”,SelectedDepartment); int departmentID=SelectedDepartment.GetValueOrDefault(); IQueryable课程=数据库课程 .其中(c=>!SelectedDepartment.HasValue | | c.DepartmentID==DepartmentID) .OrderBy(d=>d.CourseID) .包括(d=>d.部门); var sql=courses.ToString(); 返回视图(courses.ToList()); } ...
如果课程实体中有一个属性,如courseradio

 @Html.RadioButtonFor(modelitem => item.courseradio,<value you want to send to controller>)
@Html.radiobutton(modelitem=>item.courseradio,)
例如 如果要使用单选按钮发送男性或女性

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

}
@model HotelReservationSystem.Models.User
@使用(Html.BeginForm(“操作”、“控制器”))
{
@RadioButton(model=>model.gender,“Male”,新的{id=“Male”})
@标签(“男性”、“男性”)
@RadioButton(model=>model.gender,“Female”,新的{id=“Female”})
@标签(“女性”、“女性”)
@Html.ValidationMessageFor(model=>model.gender,“,new{@class=“red text”})
}

也许你可以从这里得到答案。@Html.RadioButton()和@Html.RadioButton()在用法和行为上有什么区别?
RadioButton()
与model属性绑定,而
RadioButton()
与model属性绑定not@jerryh91在名为Selected的模型中添加一个
bool
属性,并使用
RadioButton()“与模型财产绑定”是指?