【问题标题】:How do I implement Radio button group using asp.net mvc如何使用 asp.net mvc 实现单选按钮组
【发布时间】:2017-04-27 08:53:20
【问题描述】:

我的项目中有 Course, Offered Course , Course Registration 表, Section Table 我想实现每个用户的课程注册系统。这是我项目的数据库图。 我想像这样实现这个功能。我没有实现

的这个功能
   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; }

}

这是我的课程模型,

   public class Department
   {
    [Key]
    public int DepartmentId { get; set; }
    [Required]
    public string Name { get; set; }
    [Required]
    public string Code { get; set; }
    public virtual ICollection<ApplicationUser> ApplicationUsers { get; set; }
    public virtual ICollection<Course> Courses { get; set; }
}

这是我的部门模型,

     public class OfferedCourse
{
    [Key]
    public int OfferedCourseId { get; set; }
    public int Capacity { get; set; }
    public bool Status { get; set; }
    public int CourseId { get; set; }
    [ForeignKey("CourseId")]
    public virtual Course Course { get; set; }
    public string TeacherId { get; set; }
    [ForeignKey("TeacherId")]
    public virtual ApplicationUser ApplicationUser { get; set; }
    public int SectionId { get; set; }
    [ForeignKey("SectionId")]
    public virtual Section Section { get; set; }
    public int SemesterId { get; set; }
    [ForeignKey("SemesterId")]
    public virtual Semester Semester { get; set; }
    public virtual ICollection<CourseRegistration> CourseRegistrations { get; set; }

}

这是我每学期开设的课程。

     public class Section
{
    [Key]
    public int SectionId { get; set; }

    public string Title { get; set; }



}

这是我的部分模型。在这个项目中,我使用 asp.net 身份框架来管理像老师、学生这样的用户。但是尝试了很多方法 管理课程注册视图中的单选按钮。我该如何实现这个功能,因为我有不同类型的数据,比如部分和课程名称我该如何处理这个

【问题讨论】:

  • 您的图片显示的是复选框,而不是单选按钮。假设您确实想按照模型的建议选择多个课程,请参阅 this answer 以获取视图模型和视图的典型示例
  • 是的,我需要单选按钮.. 我该怎么做.. 我想我需要一个视图模型,但我该如何实现
  • 抱歉,无法从您所展示的内容中理解。 “理学士……”是您的Section.Title 财产吗?它下面的 3 项是 OfferedCourses 对应的 Section 你只想选择这 3 项中的一项吗?
  • 每个主题都有很多部分,如 A、B 或 C、d。我想这样显示:Algorithm and al section of algorir using radio button

标签: asp.net-mvc razor view asp.net-mvc-5 viewmodel


【解决方案1】:

我认为它易于使用的单选按钮组... 看这个链接http://www.w3schools.com/html/tryit.asp?filename=tryhtml_form_radio

但是对于注册页面,您应该在 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');
        }
    });
});

查看:

<form> <input type="radio" name="reg" value="male" id="radio1" checked> Male<br> <input type="radio" name="reg" value="female" id="radio2"> Female<br> <input type="radio" name="reg" value="other" id="radio3"> Other <br> <input type="Button" value="Register" id="register-btn"> </form>

【讨论】:

  • 首先我需要视图我该如何处理?
【解决方案2】:

如果您使用的是 mvc razor,则可以使用 foreach 生成 RadioButton。像这样:

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

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

【讨论】:

  • 但部分是另一个表
  • 每个主题都有很多部分....我需要为单个主题的每个部分生成单选按钮
  • 我还没有写任何课程注册视图模型,因为我不明白需要做什么。但是课程注册视图模型具有这些属性 studentId、providedcourseId.. 因为我可以使用 offerCourseId 找到该部分。
  • @HoqueMDZahidul 你能复制粘贴你的查看代码吗? (cshtml 文件) 来验证您使用的模型并查看您是如何尝试生成这些单选按钮的
猜你喜欢
  • 2015-03-19
  • 2016-04-16
  • 1970-01-01
  • 2016-07-30
  • 2010-12-07
  • 1970-01-01
  • 1970-01-01
  • 2021-07-17
  • 1970-01-01
相关资源
最近更新 更多