c# - 如何将下拉列表的多个值从视图发送到控制器
问题描述
我正在 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>
}
}
这是查看页面的结果
注意:基本上我想在提交点击时将下拉列表的多个值发送到控制器。
解决方案
推荐阅读
- java - 如何在 SQLite 中从 ImageAdapter 中保存 StringArray
- python - 如何将一个系列分配给数据框列的一部分,将另一个系列分配给数据框列的另一部分?
- google-api - 创建 OAuth 2.0 客户端 ID 页面字段被禁用
- asp-classic - 为什么在 Windows 10 上的经典 ASP 中调用 comp+ dll 方法会生成错误号 -2147164123 而没有描述?
- python - 找到将两个集合划分为 n 个大小为 m 的 bin 的所有方法,其中必须包含第一个集合(但不是第二个)中的所有项目
- ios - 使用 textViewDidChange 限制 UITextView 中的字符
- ruby-on-rails - 如何从不同的控制器调用设计认证?
- apache-nifi - 动态更新现有 WorkFlow 时是否需要停止 NiFi?
- java - 如何在运行时显示/隐藏 JavaFX 应用程序 macOS 停靠图标?
- c# - 在 C# 中将两个 JSON 结构合二为一