繁体   English   中英

C#MVC剃须刀页面foreach

[英]C# MVC razor page foreach

我已经使用stackoverflow很长时间了,只是从未问过一个问题,但是我现在需要一些建议,而且似乎找不到答案!

我的问题是,如何从我的讲师模型中传递ClassId以匹配我的班级模型中的ClassId。 换句话说,当我点击“查看花名册”时,我只希望看到该特定班级的学生? 我希望这是有道理的。 例如,老师将登录,查看他/她正在教的所有课程,并能够查看由classID标识的每个课程的花名册。

我没有使用实体框架。 仅显示来自在MSSQL上运行的存储过程的数据。 我认为我追求的是剃刀页面浏览量? 这是我到目前为止的示例,谢谢!!!

这是我的ClassModel:

     public class ClassModel
  { 
     [Display(Name = "SID")]
     public int SID { get; set; }

     [Display(Name = "Name")]
     public string FullName { get; set; }


     public int ClassID { get; set; }

}

这是我的讲师模型

   public class InstructorModel
{
    [Display(Name= "Course Title")]
    public string CourseTitle{ get; set; }

    [Display(Name = "Class ID")]
    public int ClassID { get; set; }

    [Display(Name = "Room")]
    public string Room { get; set; }

    [Display(Name = "Start Time")]
    public DateTime StartTime { get; set; }


    public int InstructorSID { get; set; }


}

讲师控制器

  public class InstructorController : Controller
{

    // GET: Course Details

    DAL.DB dblayer = new DAL.DB();
    public ActionResult Index(InstructorModel InstructorModel)

    {
        DataSet ds = dblayer.GetInstructor(InstructorModel.InstructorSID); 
        ViewBag.general = ds.Tables[0];


        return View();
    }
}

类控制器

  public class ClassController : Controller
{

    // GET: Students in Class

    DAL.DB dblayer = new DAL.DB();
    public ActionResult Index(ClassModel ClassModel)

    {
        DataSet ds1 = dblayer.GetClass(ClassModel.ClassID);
        ViewBag.general = ds1.Tables[0];

        return View();
    }
}

和我的看法

 @model IEnumerable<ABEAttendanceV3.Models.InstructorModel>
 <div class="table-responsive">
<table id="tablereport" class="table table-striped table-bordered table-hover table-condensed">
    <thead style="background-color:black; font-weight:bold; color:aliceblue">
        <tr>

    </thead>

    <thead style="background-color:black; font-weight:bold; color:aliceblue">
        <tr>
            <th>Course Title</th>
            <th>Class ID</th>
            <th>Room</th>
            <th>Start Time</th>


        </tr>
    </thead>
    <tbody>
        @{
            foreach (System.Data.DataRow dr in ViewBag.general.Rows)
            {
                <tr>
                    <td>@dr["CourseTitle"]</td>
                    <td>@dr["ClassID"]</td>
                    <td>@dr["Room"]</td>
                    <td>@dr["StartTime"]</td>

                    <td>
                        @Html.ActionLink("View Roster", "Class", new { id = ViewBag.ClassID})
                    </td>

                </tr>
            }

        }
    </tbody>
</table>

而不是传递ClassModel,而是传递ClassId的查询字符串并获取该Classid并执行所需的操作,这样您的代码将被修改为:

@{
        foreach (System.Data.DataRow dr in ViewBag.general.Rows)
        {
            <tr>
                <td>@dr["CourseTitle"]</td>
                <td>@dr["ClassID"]</td>
                <td>@dr["Room"]</td>
                <td>@dr["StartTime"]</td>

                <td>
                    @Html.ActionLink("View Roster", "Class", new { id = dr["ClassId"]})
                </td>

            </tr>
        }

    }

并在您的控制器内查看Roster ActionMethod即可

   public ActionResult ThankYou(string ClassId) {
        return RedirectToAction("Index","Class", new { id = ClassId });

}

然后在类控制器上:

DAL.DB dblayer = new DAL.DB();
public ActionResult Index(string id)

{
    DataSet ds1 = dblayer.GetClass(id);
    ViewBag.general = ds1.Tables[0];

    return View();
}

希望这有意义

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM