Asp.net mvc 如何在编辑视图中使用DropDownListFor

Asp.net mvc 如何在编辑视图中使用DropDownListFor,asp.net-mvc,razor,Asp.net Mvc,Razor,您好,我对编辑视图上的DropDownListFor有问题 基本上,我使用的是包含表单的局部视图,在我的编辑和创建视图中,我称之为局部视图 我有大约5个类似的DropdownlistFor,它们在创建操作上很好地工作,但在编辑中没有,主要是我没有(无法)设置所选的值 @Html.DropDownListFor(model => model.BAOfficerSelected, new SelectList(Model.BAOfficers, "StaffId", "Name", (Mode

您好,我对编辑视图上的DropDownListFor有问题

基本上,我使用的是包含表单的局部视图,在我的编辑和创建视图中,我称之为局部视图

我有大约5个类似的DropdownlistFor,它们在创建操作上很好地工作,但在编辑中没有,主要是我没有(无法)设置所选的值

@Html.DropDownListFor(model => model.BAOfficerSelected, new SelectList(Model.BAOfficers, "StaffId", "Name", (Model.BAOfficer!= null ? Model.BAOfficer.StaffId:-1)), new { @class = "rounded indent" })
@Html.ValidationMessageFor(model => model.BAOfficer.StaffId)
在编辑操作(GET)中,如果真实对象已填充属性,则填充属性ViewModel

if(icb.BAOfficer != null)
editICB.BAOfficer = icb.BAOfficer;


List<Staff> staffs = _fireService.GetAllStaffs().ToList();
staffs.Insert(0, new Staff { StaffId = -1, Name = "" });
editICB.BAOfficers = staffs;

return View(editICB);

我经常发现使用
列表
比使用
选择列表
效果更好

此外,我通常在下拉列表中使用
编辑器或模板
,以保持视图整洁

因此,如果我的选择列表返回
list

然后在您的
编辑器模板中

<!-- EditorTempaltes\DropDownList.cshtml -->
@model System.String
<p>
    @Html.LabelFor(m => m):
    @Html.DropDownListFor(m => m, new SelectList(
        (List<SelectListItem>)ViewData["selectList"], "Value", "Text", 
        String.IsNullOrEmpty(Model) ? String.Empty : Model), String.Empty)
    @Html.ValidationMessageFor(m => m)
</p>
@Html.EditorFor(m => m.BAOfficerSelected, "DropDownList", 
                new { selectList = Model.BAOfficers() })

最好、最干净的方法是在服务器端的
SelectList
对象中设置所选值


因此,如果您的
BAOfficerSelected
可为空。。。更简单的是:您不需要依赖于添加一个虚拟项来保存非选定值的-1

@Html.DropDownListFor(model => model.BAOfficerSelected, new SelectList(Model.BAOfficers, "StaffId", "Name", (Model.BAOfficer!= null ? Model.BAOfficer.StaffId:-1)), new { @class = "rounded indent" })
@Html.ValidationMessageFor(model => model.BAOfficer.StaffId)
相反,您可以这样做:

List<Staff> staffs = _fireService.GetAllStaffs().ToList();
editICB.BAOfficers = new SelectList(staffs, "StaffId", "Name", editICB.BAOfficer != null ? editICB.BAOfficer.StaffId : null);

需要添加第三个参数来指示默认值(如果未选择任何内容)是该文本

我解决了为我的
模型设置值的问题。在编辑操作中,BAOfficerSelected
,这是(简单的)秘密

我需要像空选项一样的第一个项目,因为这不是必需的信息,但在“编辑”视图上,如果有值,我需要将其设置为“选定”选项

最后,这是我的密码

我的型号

public int BAOfficerSelected { get; set; }
public SelectList BAOfficers { get; set; }`
我的控制器创建/编辑操作

if (icb.BAOfficer != null) // only for edit action
 editICB.BAOfficerSelected = icb.BAOfficer.StaffId; //this will set the selected value like a mapping


//for Edit and Create
List<Staff> staffs = _fireService.GetAllStaffs().ToList();
staffs.Insert(0, new Staff { StaffId = -1, Name = "" });

editICB.BAOfficers = new SelectList(staffs, "StaffId", "Name");

return View(editICB);`

我希望这能帮助其他人。

你能发布你是如何构建
模型的吗。BAOfficers
?我用它编辑了文章。看起来不错,但所选值的条件有以下错误<代码>编辑b.BAOfficer!=无效的editICB.BAOfficer.StaffId:null无法确定条件表达式的类型,因为“int”和“
BAOfficerSelected”之间没有隐式转换
需要为可为null的int(
int?
)。你能改变它的类型吗?当我看到错误时我改变了,但没有修复。错误在icb.IncidentCommander.StaffId中:整行为空
editICB.BAOfficers=new SelectList(staff.Where(s=>s.Role==RoleTypes.IncidentCommander).ToList(),“StaffId”,“Name”,editICB.IncidentCommander!=null?**icb.IncidentCommander.StaffId:null**)我改为
editICB.BAOfficers=new SelectList(职员,“StaffId”,“Name”,icb.BAOfficer!=null?(int?)icb.BAOfficer.StaffId:null)。我做了一个隐式转换使用(int?)和部分作品。。。我在视图中进行了检查,我看到BAOfficers已填充,并且有一个选定的值。但是Drop淹死列表看起来不尊重它,因为它是在渲染时选择了“选择一个…”。删除代码(icb.BAOfficer!=null?(int?)icb.BAOfficer.StaffId:null)并硬编码现有的int值。。。我只是想看看它是否有效。我将如何将我的列表转换为列表?我只是可以看到它使用循环,但不好看使用foreach从列表读取和填充列表。您的想法是什么?@CidaoPapito我添加了一个示例,说明如何在
staff
对象上使用Linq表达式来构建列表。谢谢。我找到了一个解决问题的方法,并发布了它。我将检验你的建议,并将其设置为有用的。好啊我需要学习编辑模板,这样我就可以开始了。
public int BAOfficerSelected { get; set; }
public SelectList BAOfficers { get; set; }`
if (icb.BAOfficer != null) // only for edit action
 editICB.BAOfficerSelected = icb.BAOfficer.StaffId; //this will set the selected value like a mapping


//for Edit and Create
List<Staff> staffs = _fireService.GetAllStaffs().ToList();
staffs.Insert(0, new Staff { StaffId = -1, Name = "" });

editICB.BAOfficers = new SelectList(staffs, "StaffId", "Name");

return View(editICB);`
@Html.DropDownListFor(model => model.BAOfficerSelected, Model.BAOfficers, new { @class = "rounded indent" })