asp.net 发送下拉列表和列表的值

xtupzzrd  于 2023-11-20  发布在  .NET
关注(0)|答案(3)|浏览(181)

我有三个模型:

public class Student {
    public int StudentId { get; set; }
    public string StudentName { get; set; }
    public string Father { get; set; }
    public string PhoneNumber { get; set; }
    public string Email { get; set; }
    public bool Gender { get; set; }
    public int EmployID { get; set; }
    public int RankID  { get; set; }
    public int CollegeID { get; set; }
    public string Address { get; set; }
    public string Description { get; set; }

    public virtual ICollection<StudentCourse> OstadCourses2 { get; set; }

    public Student()
    {

    }

}

public class Course
{

    public int CourseId { get; set; }
    public string Title { get; set; }
    public string Subject { get; set; }
    public int Year { get; set; }
    public virtual ICollection<StudentCourse> OstadCourses1 { get; set; }

    public Course()
    {

    }

}

public class StudentCourse
{
    [Key]
    public int StudentCourseId { get; set; }
    public int StudentId { get; set; }
    public int CourseId { get; set; }

    public virtual Ostad Ostad14 { get; set; }
    public virtual Course Course14 { get; set; }

    public OstadCourse()
    {
    }

    public OstadCourse(int ostadId, int courseId)
    {
        OstadId = ostadId;
        CourseId = courseId;
    }
}

字符串
我已经创建了一个学生列表。我想创建一个视图,其中有一个课程和学生的属性列表。然后我们点击一个按钮,在数据库中绑定Courseid和学生ID,并显示学生和课程的列表。
我试过了。但是没有成功。我怎么才能修好它呢?

2jcobegt

2jcobegt1#

在你随后的描述中,
我想创建一个视图,有一个课程和学生与他们的属性列表。然后我们点击一个按钮,绑定数据库中的课程ID和学生ID,并显示学生和课程的列表
下面是我为您提供的部分实现代码,您可以将其用作参考。
我的控制器:

public class StudentCourse : Controller
{
     private readonly SchoolDbcontext _context;

     public StudentCourse(SchoolDbcontext context)
     {
         _context = context;
     }
     public ActionResult StudentList()
   {
       // Retrieve a list of students and courses from your database
       var students = _context.Students.Include(s => s.CourseStudents).ThenInclude(cs => cs.Course).ToList();
       var courses = _context.Courses.ToList();
       var viewModel = new StudentCourseViewModel
       {
           Students = students,
                 
           Courses = new SelectList(courses, "CourseId", "Subject").ToList()
       };
            

       return View(viewModel);
   }

     [HttpPost]
     public ActionResult BindStudentToCourse(int SelectedCourseId, int[] SelectedStudents) //
     {
         if (SelectedCourseId != 0 && SelectedStudents != null)
         {
             foreach (int studentId in SelectedStudents)
             {
                 var existingRecord = _context.CourseStudents.FirstOrDefault(cs => cs.CourseId == SelectedCourseId && cs.StudentId == studentId);
                 if (existingRecord == null)
                 {
                     var courseStudent = new CourseStudent
                     {
                         StudentId = studentId,
                         CourseId = SelectedCourseId
                     };

                     _context.CourseStudents.Add(courseStudent);
                 }

                 _context.SaveChanges();
             }

         }

             return RedirectToAction("StudentList");
         }
     }
}

字符串
我的页面:

@using (Html.BeginForm("BindStudentToCourse", "StudentCourse", FormMethod.Post))
{
    <div class="form-group">
        @Html.LabelFor(model => model.SelectedCourseId, "Select a Course:")
        @Html.DropDownListFor(model => model.SelectedCourseId, Model.Courses, "Select a Course", new { @class = "form-control" })
    </div>

    <table class="table">
        <tr>
            <th> Studentname</th>
            <th> Course</th>
        </tr>
        @for (int i = 0; i < @Model.Students.Count; i++)
        {
         
            <tr>
                <td>@Model.Students[i].StudentName</td>
                <td>
                    <ul>
                        @foreach (var course in Model.Students[i].CourseStudents)
                        {
                            <li>@course.Course.Subject</li>
                        }
                    </ul>
                </td>
                
                <td>
                    @Html.CheckBox("SelectedStudents", new { value = Model.Students[i].StudentId })
                    @Html.Hidden("Students[" + i + "].StudentId", Model.Students[i].StudentId)
                </td>
            </tr>
        }
    </table>

    <button type="submit" class="btn btn-primary">Bind Students to Course</button>


当我选择三个学生绑定到一个课程时:

当我点击按钮:

我可以得到课程ID和学生谁选择了我。
最终,学生和课程成功连接:

fsi0uk1n

fsi0uk1n2#

在您的问题描述中,我了解到您可能面临两种情况。
在第一个场景中,您需要两个列表,一个用于学生,另一个用于课程。单击后,您希望将学生绑定到特定课程并获得相应的更新列表。
在第二个方案中,您希望通过从学生列表中课程列的选项卡中进行选择,将每个学生与他们各自的课程相关联。

szqfcxe2

szqfcxe23#

我创建了以下视图来回答我的问题:

@model DataLayer.OstadCourse

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>

@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()
    
<div class="form-horizontal">
    <h4>OstadCourse</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })

    <div class="form-group">
        @Html.LabelFor(model => model.CourseId, "CourseId", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("CourseId", null, htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.CourseId, "", new { @class = "text-danger" })
        </div>
    </div>

    @*<div class="form-group">
        @Html.LabelFor(model => model.OstadId, "OstadId", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("OstadId", null, htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.OstadId, "", new { @class = "text-danger" })
        </div>
    </div>*@

    <div class="form-group">
        @Html.LabelFor(model => model.OstadName, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.OstadName, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.OstadName, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.OstadFamily, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.OstadFamily, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.OstadFamily, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Father, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Father, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Father, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.PhonNumber, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.PhonNumber, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.PhonNumber, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Kodmelli, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Kodmelli, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Kodmelli, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Gender, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            <div class="checkbox">
                @Html.EditorFor(model => model.Gender)
                @Html.ValidationMessageFor(model => model.Gender, "", new { @class = "text-danger" })
            </div>
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.EmployName, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.EmployName, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.EmployName, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.AcademicRank, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.AcademicRank, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.AcademicRank, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.CollegeName, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.CollegeName, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.CollegeName, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Address, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Address, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Address, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
        </div>
    </div>


    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>
</div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

<script src="~/Scripts/jquery-3.4.1.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>

字符串

相关问题