如何从选定的存储值返回mvc3的下拉列表中的数据 [英] how can i return data in dropdown list in mvc3 from selected store value

查看:84
本文介绍了如何从选定的存储值返回mvc3的下拉列表中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有emp表和empno,名称,deptno(部门表中的fk)列.当我插入新员工时,我会在下拉列表中输入名称和部门编号.我可以在下拉列表中显示部门名称,并且可以在emp表中存储部门编号.但是我的问题是,当我尝试编辑特定员工时,我无法在下拉列表中绑定该特定部门编号,名称. />
在[httpget]
我得到了ID,名称(没有问题),我的下拉部门列表应该是返回的,部门名称没有保存,然后如果我想要的话,我可以更改部门(现在我加载该列表,但它选择了第一个值,如果我不这样做) '不更改部门,然后更新部门第一个值,因为它已经被选择)

Hi, i have emp table and empno,name,deptno(fk from department table)column.when i insert new employee i give name and department no in drop down list. i can display department name in dropdown list and i can store department no in emp table.but my problem is when i try to edit the particular employee then i can''t bind that particular department no,name in drop down list.

in [httpget]
i get id, name (no problem)and my drop down department list should be return save department no,name and then if i want then i can change department(now i load that list but it selected the first value and if i don''t change department then it update department first value as it is already selected)

推荐答案

如果您使用的是Razor Views
在View中使用optionlabel
If you are using Razor Views
Use optionlabel in View
@Html.DropDownListFor(model => model.FieldName, (SelectList)ViewBag.DepartMent, Model.FieldName)


或创建SelectList并在操作中提及您的selectedValue


or Create SelectList and mention your selectedValue in Action

SelectList Banks = new SelectList(new Department().Seed(), "DepartmentName", "DepartmentId", "SelectedValue");


谢谢您的建议.其实我做错了.当我创建助手类时,我给它起了名字叫dept,但是当我叫这个名字时,我叫deptid而不是dept.so,我更正了它并且可以工作.
Thank your suggestion. actually i did mistake. when i create helper class i give name dept but when i call this i call deptid instead of dept.so, i correct it and it works.


这篇关于如何从选定的存储值返回mvc3的下拉列表中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆