Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用DropDownList在视图中编辑多条记录_C#_Asp.net Mvc - Fatal编程技术网

C# 使用DropDownList在视图中编辑多条记录

C# 使用DropDownList在视图中编辑多条记录,c#,asp.net-mvc,C#,Asp.net Mvc,我可以从一个控制器编辑多条记录,但是在视图中编辑多条记录时,我无法成功添加DropDownList 控制器: public ActionResult ReportByNumber() { ViewBag.Department = new SelectList(from r in db.Departments select r, "Name", "Name"); return View(reportList.ToList()); } 查看允许多条记录: @for (int i

我可以从一个控制器编辑多条记录,但是在视图中编辑多条记录时,我无法成功添加
DropDownList

控制器:

public ActionResult ReportByNumber() 
{
    ViewBag.Department = new SelectList(from r in db.Departments select r, "Name", "Name");
    return View(reportList.ToList());
}
查看允许多条记录:

@for (int i = 0; i < Model.Count; i++) { 

@Html.TextBoxFor(m => m[i].Department, new { @style = "width:120px" })

}
@for(inti=0;im[i].部门,新{@style=“width:120px”})
}
有没有办法让部门成为一个下拉列表?

试试这个: 你的第一控制器

public ActionResult ReportByNumber() 
{
ViewBag.DepartmentID = new SelectList(from r in db.Departments select r, "DepartmentID", "Name",selectedDepartment);
return View();
}
public ActionResult ReportByNumber() 
{
  var departments = (from r in db.Departments select r).ToList();
  ViewBag.DepartmentList = new SelectList(departments, "Name", "Name");
  return View(reportList.ToList());
}
在你看来是第二位

@Html.DropDownList("DepartmentID", null, htmlAttributes: new { @class = "form-control" })
#更新1 这是如何绑定您的下拉列表

 [HttpPost]
    public ActionResult ReportByNumber([Bind(Include = "DepartmentID")]Departments dept)
    {
    //......
    }

在集合中使用
@Html.DropDownList()
时,需要使用编辑器模板(或在每次迭代中重建
选择列表
,这不是很有效)。假设你的模型是

public class ReportModel
{
  [Required(ErrorMessage = "Please select a department")]
  public string Department { get; set; }
  ....
}
然后在控制器中

public ActionResult ReportByNumber() 
{
ViewBag.DepartmentID = new SelectList(from r in db.Departments select r, "DepartmentID", "Name",selectedDepartment);
return View();
}
public ActionResult ReportByNumber() 
{
  var departments = (from r in db.Departments select r).ToList();
  ViewBag.DepartmentList = new SelectList(departments, "Name", "Name");
  return View(reportList.ToList());
}
EditorTemplate(
/Views/Shared/EditorTemplates/ReportModel.cshtml

主视图

@model List<ReportModel>
@using(Html.BeginForm())
{
  @Html.EditorFor(m => m, new { DepartmentList= ViewBag.DepartmentList})
  <input type="submit" />
}

谢谢Stephen和Feras让我走上正轨

我最终在视图中使用了以下代码:

@Html.DropDownListFor(m => m[i].Department, (IEnumerable<SelectListItem>)ViewBag.Department, item.Department ?? "")
@Html.DropDownListFor(m=>m[i].Department,(IEnumerable)ViewBag.Department,item.Department??)

显示
报告列表的定义
请编辑您的标题。请参见“”,其中共识是“不,他们不应该”。Teo Van Kot,var reportList=\u db.reportModels.where(r=>r.id>=reportNumLow);谢谢你的回答。我得测试一下。当我第一次尝试时,我能够显示DropDownList,但它不会将值保存到Department字段。在视图中,我正在编辑多条记录,这是通过for循环完成的。我不确定是否有办法将[I]添加到部门字段。很抱歉,您迟到了,是否可以添加您的完整视图代码,尤其是您的循环使用此代码您不需要将[I]添加到部门字段查看我的更新答案清楚您没有阅读或不理解我的答案。这将无法正常工作。无论您将每个
部门
属性的值设置为什么,当首次加载页面时,将为每个项目选择第一个选项,而不是您将值设置为的选项。谢谢Stephen,我将进一步研究您的答案并测试代码。