Asp.net mvc 如何使用asp.net mvc实现单选按钮组

Asp.net mvc 如何使用asp.net mvc实现单选按钮组,asp.net-mvc,razor,view,asp.net-mvc-5,viewmodel,Asp.net Mvc,Razor,View,Asp.net Mvc 5,Viewmodel,我有课程,提供课程,课程注册表,节表在我的项目中,我想实现每个用户的课程注册系统。这是我的项目数据库图。 我想这样实现这个特性。我没有实现的这个特性 class time . public class Course { [Key] public int CourseId { get; set; } [Required] public string Name { get; set; } public string Code { get; set; }

我有课程,提供课程,课程注册表,节表在我的项目中,我想实现每个用户的课程注册系统。这是我的项目数据库图。 我想这样实现这个特性。我没有实现的这个特性

   class time . public class Course
{
    [Key]

    public int CourseId { get; set; }
    [Required]
    public string Name { get; set; }
    public string Code { get; set; }
    public string Credit { get; set; }
    public DateTime RegDate { get; set; }

    public string PreCourse { get; set; }
    public int DepartmentId { get; set; }
    [ForeignKey("DepartmentId")]
    public virtual Department Department { get; set; }
    public virtual ICollection<OfferedCourse> OfferedCourses { get; set; }

}
这是我的分区模型。在这个项目中,我使用asp.net身份框架来管理教师、学生等用户。但是我已经尝试了很多方法
管理“课程注册”视图中的单选按钮。由于我有不同类型的数据,如章节和课程名称,我如何实现此功能?我如何处理此问题?我认为它易于使用单选按钮组。。。 请参阅此链接

但对于注册页面,您应该使用javascript中的
if-else
代码来处理您的请求:

<script>
$('#register-btn').click(function () {
    var regType = 0;
    if ($('#radio1').is(":checked")) {
        regType = 1;
    }
    if ($('#radio2').is(":checked")) {
        regType = 2;
    }
    if ($('#radio3').is(":checked")) {
        regType = 3;
    }
    $.ajax({
        type: "POST",
        url: "/Registration/reg_Ajax",
        contentType: "application/json; charset=utf-8",
        data: '{type: "' + regType + '"}',
        dataType: "text",
        success: function (response) {
            alert(response);
        },
        failure: function (response) {
            alert("fail");
        },
        error: function (request, status, error) {
            alert('error');
        }
    });
});

$(“#注册btn”)。单击(函数(){
var regType=0;
如果($('#radio1')。为(“:选中”)){
regType=1;
}
如果($('#radio2')是(“:选中”)){
regType=2;
}
如果($('#radio3')是(“:选中”)){
regType=3;
}
$.ajax({
类型:“POST”,
url:“/Registration/reg_Ajax”,
contentType:“应用程序/json;字符集=utf-8”,
数据:'{type:'+regType+''}',
数据类型:“文本”,
成功:功能(响应){
警报(响应);
},
故障:功能(响应){
警报(“失败”);
},
错误:功能(请求、状态、错误){
警报(“错误”);
}
});
});

视图:


男性
女性
其他

我认为它很容易使用单选按钮组。。。 请参阅此链接

但对于注册页面,您应该使用javascript中的
if-else
代码来处理您的请求:

<script>
$('#register-btn').click(function () {
    var regType = 0;
    if ($('#radio1').is(":checked")) {
        regType = 1;
    }
    if ($('#radio2').is(":checked")) {
        regType = 2;
    }
    if ($('#radio3').is(":checked")) {
        regType = 3;
    }
    $.ajax({
        type: "POST",
        url: "/Registration/reg_Ajax",
        contentType: "application/json; charset=utf-8",
        data: '{type: "' + regType + '"}',
        dataType: "text",
        success: function (response) {
            alert(response);
        },
        failure: function (response) {
            alert("fail");
        },
        error: function (request, status, error) {
            alert('error');
        }
    });
});

$(“#注册btn”)。单击(函数(){
var regType=0;
如果($('#radio1')。为(“:选中”)){
regType=1;
}
如果($('#radio2')是(“:选中”)){
regType=2;
}
如果($('#radio3')是(“:选中”)){
regType=3;
}
$.ajax({
类型:“POST”,
url:“/Registration/reg_Ajax”,
contentType:“应用程序/json;字符集=utf-8”,
数据:'{type:'+regType+''}',
数据类型:“文本”,
成功:功能(响应){
警报(响应);
},
故障:功能(响应){
警报(“失败”);
},
错误:功能(请求、状态、错误){
警报(“错误”);
}
});
});

视图:


男性
女性
其他

如果您使用的是mvc razor,您可以使用foreach生成单选按钮。大概是这样的:

//HTML code here
@foreach (var offeredCourse in Model.OfferedCourses)
{       
    @Html.RadioButton(offeredCourse.OfferedCourseId.ToString(), offeredCourse.ID)          
}
//HTML code here

但我不确定你用的是哪种型号。请粘贴您的课程注册视图代码,以便更具体地使用此答案

如果您使用的是mvc razor,您可以使用foreach生成单选按钮。大概是这样的:

//HTML code here
@foreach (var offeredCourse in Model.OfferedCourses)
{       
    @Html.RadioButton(offeredCourse.OfferedCourseId.ToString(), offeredCourse.ID)          
}
//HTML code here


但我不确定你用的是哪种型号。请粘贴您的课程注册查看代码,以便更具体地回答此问题。

首先我需要查看如何处理此问题?首先我需要查看如何处理此问题?但分区是另一个表格每个科目都有多个分区…我需要为每个科目的每个分区生成单选按钮I不要编写任何课程注册查看模型,因为我不知道需要做什么。但课程注册视图模型具有这些属性studentId,提供了CourseID。。因为我可以使用offeredCourseId找到该部分。@HoqueMDZahidul您可以复制并粘贴视图代码吗?(cshtml文件)验证您正在使用的模型,并查看您如何尝试生成这些RadioButton但是section是另一个表每个主题都有许多section…我需要为每个主题的每个section生成单选按钮我还没有编写任何课程注册视图模型,因为我不知道需要做什么。但课程注册视图模型具有这些属性studentId,提供了CourseID。。因为我可以使用offeredCourseId找到该部分。@HoqueMDZahidul您可以复制并粘贴视图代码吗?(cshtml文件)要验证您正在使用的模型,并查看如何尝试生成这些RadioButton,您的图像将显示复选框,而不是单选按钮。假设您确实希望按照模型的建议选择多个课程,请参阅“查看模型”和“查看我需要”单选按钮的典型示例。。我想我需要一个视图模型,但是我怎么能实现呢?对不起,从你展示的内容中无法理解。“理学学士…”是您的
部分。标题
财产吗?它下面的3个项目是否为该
部分提供的
课程
您只想从这3个项目中选择一个吗?每个对象都有许多部分,如A、B或C、d。我想在视图中这样显示:算法和algorir的所有部分使用单选按钮您的图像显示复选框,而不是单选按钮。假设您确实希望按照模型的建议选择多个课程,请参阅“查看模型”和“查看我需要”单选按钮的典型示例。。我想我需要一个视图模型,但是我怎么能实现呢?对不起,从你展示的内容中无法理解。“理学学士…”是您的
部分。标题
财产吗?它下面的3个项目是否为该
部分提供的
课程
您只想从这3个项目中选择一个吗?每个对象都有许多部分,如A、B或C、d。我想在视图中这样显示:使用单选按钮显示算法和algorir的al部分
//HTML code here
@foreach (var offeredCourse in Model.OfferedCourses)
{       
    @Html.RadioButton(offeredCourse.OfferedCourseId.ToString(), offeredCourse.ID)          
}
//HTML code here