首页 > 解决方案 > 如何将下拉列表的多个值从视图发送到控制器

问题描述

我正在 asp.net 中开发学生考勤系统。当我将下拉列表的多个值传递给控制器​​时,我遇到了一个问题。我多次使用相同的下拉列表,所以请帮助我如何将多个下拉列表值发送到控制器。这是我的控制器。

 public ActionResult TakeAttendence(FormCollection c)
    {     ViewData["c_id"] = new SelectList(db.ClassTbs, "c_id", "Class_Name");
          int cid=Int32.Parse(Request.Form["c_id"].ToString());

         //this viewdata is used for display status on view
          ViewData["a_id"] = new SelectList(db.AttendenceValues, "a_id", "Status");

         List<StudentTb> stu = db.StudentTbs.ToList();
        List<ClassTb> cls = db.ClassTbs.ToList();
        List<Attendence> att = db.Attendences.ToList();

        var res = from s in stu
                  where s.c_id == cid
                  join cl in cls on s.c_id equals cl.c_id into table1
                  from cl in table1.DefaultIfEmpty()
                  join a in att on s.s_id equals a.studentId into table2
                  from a in table2.DefaultIfEmpty()

                  select new GeneralClass { studenttb = s, classtb = cl , attendencetb = a };
        List<GeneralClass> g = res.ToList();
        foreach(var cd in g)
        {
            cd.studenttb.s_Date = DateTime.Now;
           // cd.attvalue = db.AttendenceValues.ToList();
           // cd.studenttb.attval = db.AttendenceValues.ToList();
        }

        return View(g);
       // return View(res);
    }
//this is post method
  [HttpPost]
    public ActionResult SubmitAttndence(List<GeneralClass> model )
    {
          foreach (var i in model)
         {
            var userRows = db.Attendences.Where(x => 
             x.StudentTb.s_id.Equals(i.studenttb.s_id)).SingleOrDefault();

            if (userRows == null)
            {
                Attendence userRow = new Attendence();
                userRow.Date = i.studenttb.s_Date;


           userRow.Status=Int32.Parse(Request.Form["a_id"].ToString());//here 
           //I received dropdown list values it work fine when I take only 
           //one 
           //student attendance but when students are two or more it show 
             //null 
           //values
                userRow.Remarks = i.studenttb.s_remarks;
                userRow.studentId = i.studenttb.s_id;
                db.Attendences.Add(userRow);
                db.SaveChanges();

            }}
             return view();}

这是我的观点

      @using (@Html.BeginForm("SubmitAttndence", "Student", FormMethod.Post))
      {
       <table>
        <tr>
        <th>s_id</th>
        <th>first Name</th>
        <th>Last Name</th>
        <th>Date</th>
        <th>Remarks</th>

        <th>Status</th>
    </tr>
    @if (Model != null)
    {
      for (int i = 0; i < Model.Count; i++)
       {
        <tr>
            <td>
                @Html.HiddenFor(model => model[i].studenttb.s_id)
            </td>
        <td>
            @Html.EditorFor(model => model[i].studenttb.F_Name, new { 
            htmlAttributes = new { @readonly = "readonly" } })
        </td>
        <td>
        @Html.EditorFor(model => model[i].studenttb.L_Name, new { 
        htmlAttributes = new { @readonly = "readonly" } })
        </td>
        <td>
        @Html.EditorFor(model => model[i].studenttb.s_Date,  new { 
        htmlAttributes = new { @readonly = "readonly" } })
        </td>
        <td>
        @Html.TextBoxFor(model => model[i].studenttb.s_remarks)
        </td>
        <td>
        <div class="col-md-10">
        @Html.DropDownList("a_id" , null, htmlAttributes: new { @class = 
        "form-control" })
        </div></td>

        </tr>
             }
        }

这是查看页面的结果

在此处输入图像描述

注意:基本上我想在提交点击时将下拉列表的多个值发送到控制器。

标签: c#asp.netasp.net-mvchtml.dropdownlistfor

解决方案


推荐阅读