Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.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
Asp.net mvc 3 如何将列绑定到DropDownList并获取MVC3中选择的值?_Asp.net Mvc 3_C# 4.0_Html Select - Fatal编程技术网

Asp.net mvc 3 如何将列绑定到DropDownList并获取MVC3中选择的值?

Asp.net mvc 3 如何将列绑定到DropDownList并获取MVC3中选择的值?,asp.net-mvc-3,c#-4.0,html-select,Asp.net Mvc 3,C# 4.0,Html Select,嗨,伙计们,我有两张像这样的桌子 usertable RoleTable ----------------------- --------------------------- UserID|UserName|Pwd|RoleID RoleID|RoleName 1 |Anil |123|1

嗨,伙计们,我有两张像这样的桌子

          usertable                                 RoleTable
   -----------------------                     ---------------------------
  UserID|UserName|Pwd|RoleID                        RoleID|RoleName
    1   |Anil    |123|1                               1   |Admin
现在我在一个表中显示usertable,其中他有一个AddNew链接,当管理员单击AddNew链接时,我将向他显示AddNew页面,其中他有一些标签和文本框来添加New user

现在我想做的是在AddNew页面中,我想在下拉列表中显示所有角色名称,以便管理员可以选择用户必须担任的角色…并且我想检索所选的数据

这是我的模型课

         public class ResourceModel
{
    public static List<ResourceModel> GetList { get; set; }
    public Int16 EmployeeId { get; set; }
    public string EmployeeName { get; set; }
    public string EmployeeEmailId { get; set;}
    public string GroupName { get; set; }
    public string EmployeePassword { get; set; }

}
结束
转到

思考如何将所选角色id发回服务器

而不是
公共静态列表GetList{get;set;}
使用
public int RoleId{get;set;}
。你不能为
列表创建
@Html.DropDownListFor
,这没有意义,你只是回发了一个值

接下来,在模型中创建一个方法,返回
SelectListItem
IEnumerable

public static IEnumerable<SelectListItem> GetRoles()
    {
        var roles = //However you plan to get this data from db
        return roles.Select(o => new SelectListItem
                                     {
                                         Value = o.RoleID,
                                         Text = o.RoleName
                                     });
    } 
公共静态IEnumerable GetRoles()
{
var roles=//但是您计划从数据库获取此数据
返回角色。选择(o=>new SelectListItem
{
值=o.RoleID,
Text=o.RoleName
});
} 
最后,你认为:

@Html.DropDownList("RoleId")

@Html.DropDownListFor(x=>x.RoleId,ResourceModel.GetRoles())
考虑如何将所选角色id发回服务器

而不是
公共静态列表GetList{get;set;}
使用
public int RoleId{get;set;}
。你不能为
列表创建
@Html.DropDownListFor
,这没有意义,你只是回发了一个值

接下来,在模型中创建一个方法,返回
SelectListItem
IEnumerable

public static IEnumerable<SelectListItem> GetRoles()
    {
        var roles = //However you plan to get this data from db
        return roles.Select(o => new SelectListItem
                                     {
                                         Value = o.RoleID,
                                         Text = o.RoleName
                                     });
    } 
公共静态IEnumerable GetRoles()
{
var roles=//但是您计划从数据库获取此数据
返回角色。选择(o=>new SelectListItem
{
值=o.RoleID,
Text=o.RoleName
});
} 
最后,你认为:

@Html.DropDownList("RoleId")

@Html.DropDownListFor(x=>x.RoleId,ResourceModel.GetRoles())
使您的Get操作如下所示:

[AcceptVerbs(HttpVerbs.Get)]
public ActionResult AddNew()
{
        ViewBag.RoleId = new SelectList(GetRoles(), "RoleID", "RoleName");
        return View();
}
GetRoles
应返回
Role

在你看来:

@Html.DropDownList("RoleId")
编辑:解释

Html.DropDownList
方法将搜索
ViewBag
ViewData
实际上)以查找名为
RoleId
SelectListItem
列表,并使用该列表创建下拉列表。您不需要自己明确指定列表

编辑:更正
GetRoles

GetRoles
方法应如下所示:

public static List<Role> GetRoles() {
    SqlConnection conn = new SqlConnection("Data Source=LMIT-0039;Initial Catalog=BugTracker;Integrated Security=True");
    SqlCommand Cmd = new SqlCommand("Select GroupId,EmplopyeeRole from  EmployeeGroup", conn);
    conn.Open();
    SqlDataAdapter da = new SqlDataAdapter(Cmd);
    DataSet ds = new DataSet();
    da.Fill(ds);

    List<Role> allRoles = new List<Role>();
    for(int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++) {
        Role role = new Role {
            RoleId = Convert.ToInt16(ds.Tables[0].Rows[i]["GroupId"]),
            RoleName = ds.Tables[0].Rows[i]["EmplopyeeRole"].ToString()
        };

        allRoles.Add(role);
    }
    conn.Close();
    return allRoles;
}
公共静态列表GetRoles(){
SqlConnection conn=newsqlconnection(“数据源=LMIT-0039;初始目录=BugTracker;集成安全=True”);
SqlCommand Cmd=new-SqlCommand(“从EmployeeGroup中选择GroupId、EmployeeRole”,conn);
conn.Open();
SqlDataAdapter da=新的SqlDataAdapter(Cmd);
数据集ds=新数据集();
da.填充(ds);
List allRoles=new List();

对于(int i=0;i使您的Get操作如下:

[AcceptVerbs(HttpVerbs.Get)]
public ActionResult AddNew()
{
        ViewBag.RoleId = new SelectList(GetRoles(), "RoleID", "RoleName");
        return View();
}
GetRoles
应返回
Role

在你看来:

@Html.DropDownList("RoleId")
编辑:解释

Html.DropDownList
方法将搜索
ViewBag
ViewData
实际上)一个名为
RoleId
SelectListItem
列表,并使用该列表创建下拉列表。您无需自己明确指定该列表

编辑:更正
GetRoles

GetRoles
方法应如下所示:

public static List<Role> GetRoles() {
    SqlConnection conn = new SqlConnection("Data Source=LMIT-0039;Initial Catalog=BugTracker;Integrated Security=True");
    SqlCommand Cmd = new SqlCommand("Select GroupId,EmplopyeeRole from  EmployeeGroup", conn);
    conn.Open();
    SqlDataAdapter da = new SqlDataAdapter(Cmd);
    DataSet ds = new DataSet();
    da.Fill(ds);

    List<Role> allRoles = new List<Role>();
    for(int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++) {
        Role role = new Role {
            RoleId = Convert.ToInt16(ds.Tables[0].Rows[i]["GroupId"]),
            RoleName = ds.Tables[0].Rows[i]["EmplopyeeRole"].ToString()
        };

        allRoles.Add(role);
    }
    conn.Close();
    return allRoles;
}
公共静态列表GetRoles(){
SqlConnection conn=newsqlconnection(“数据源=LMIT-0039;初始目录=BugTracker;集成安全=True”);
SqlCommand Cmd=new-SqlCommand(“从EmployeeGroup中选择GroupId、EmployeeRole”,conn);
conn.Open();
SqlDataAdapter da=新的SqlDataAdapter(Cmd);
数据集ds=新数据集();
da.填充(ds);
List allRoles=new List();

对于(int i=0;我这里有人能帮我吗请……….这里有人能帮我吗请………@Mohayemin..我必须在我的GetRoles()中声明RoleId,RoleName吗method@anilkumar:只需在
GetRoles()中从数据库返回所有
角色的列表
方法。你的
角色
类有
角色ID
角色名
,不是吗?@Mohayemin..是的,我的类中确实有一个角色,但是如果你正确地从
GetRoles()返回了所有
角色,我会在下拉列表中清空
您不应该得到空的下拉列表。将调试指针放在第行
返回视图()
上,然后检查
ViewBag.RoleId中的内容。告诉我结果。@Mohayemin…您可以检查我的getroles()吗方法..我在我的问题中发布了这个..我已经根据你的建议更改了我的代码…@Mohayemin..我必须在我的GetRoles()中声明RoleId,RoleName吗method@anilkumar:只需在
GetRoles()中从数据库返回所有
角色的列表
方法。你的
角色
类有
角色ID
角色名
,不是吗?@Mohayemin..是的,我的类中确实有一个角色,但是如果你正确地从
GetRoles()返回了所有
角色,我会在下拉列表中清空
你不应该得到空的下拉列表。在
返回视图()
行上放置一个调试指针,检查
ViewBag.RoleId中的内容。告诉我结果。@Mohayemin…你能检查我的getroles()方法吗..我正在发布