1

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

          usertable                                 RoleTable
   -----------------------                     ---------------------------
  UserID|UserName|Pwd|RoleID                        RoleID|RoleName
    1   |Anil    |123|1                               1   |Admin

现在我在表格中显示用户表,他有一个 AddNew 链接,当管理员单击 AddNew 链接时,我将向他显示 AddNew 页面,其中他有一些标签和文本框给 AddNew 用户,

现在我要做的是在 AddNew 页面中我想在 DropDown 中显示所有 RoleNames,以便管理员可以选择用户必须是哪个角色....并且我想检索选定的数据

这是我的模型课

         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; }

}

这是我的控制器

            [AcceptVerbs(HttpVerbs.Get)]
     public ActionResult AddNew()
    {

        ViewBag.Roles = new SelectList(GetRoles(),"RoleID","RoleName");           
        return View();
    }
    //
    //Geting All Roles In a GetRoles()/
    //
      public static 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);
        for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
        {
            //roles.Add(new SelectListItem{.Value=i,.Text=i};                   
            var model = new ResourceModel();
            model.RoleId = Convert.ToInt16(ds.Tables[0].Rows[i]["GroupId"]);
            model.RoleName = ds.Tables[0].Rows[i]["EmplopyeeRole"].ToString();
            //roles.Value=Convert.ToString( model.RoleId);
            //roles.Text=model.RoleName;
        }
        conn.Close();
          return ds ;
      }

我必须在上面的代码中返回什么

这是我的 AddNew Aspx 页面

     <div>
    <% using (Html.BeginForm())
  { %>
  <%-- <form action="Create.aspx" method="post"></form>--%>
   <%:Html.ValidationSummary(true)%>
     <fieldset>
   <legend style="color:Orange; font-weight:bolder;">AddNew Project</legend>

    <div class="editor-label" style="color:Orange; font-weight:bolder;">
        <%: Html.LabelFor(model => model.EmployeeName)%>
    </div>
    <div class="editor-field">
       <%:Html.EditorFor(model => model.EmployeeName)%>
      <%: Html.ValidationMessageFor(model => model.EmployeeName)%>
    </div>

    <div class="editor-label" style="color:Orange; font-weight:bolder;">
       <%:Html.LabelFor(model => model.EmployeeEmailId)%>
    </div>
    <div class="editor-field">
       <%:Html.EditorFor(model => model.EmployeeEmailId)%>
       <%:Html.ValidationMessageFor(model => model.EmployeeEmailId)%>
    </div>
    <div class="editor-label" style="color:Orange; font-weight:bolder;">
    <%:Html.LabelFor(model => model.EmployeePassword)%>
    </div>
    <div class="editor-field">
    <%:Html.EditorFor(model => model.EmployeePassword)%>
    <%:Html.ValidationMessageFor(model => model.EmployeePassword)%>
    </div>
      <div class="editor-label" style="color:Orange; font-weight:bolder;">
    <%:Html.LabelFor(model => model.GroupName)%>
    </div>
    <div class="editor-field">
    <%:Html.EditorFor(model => model.GroupName)%>
    <%:Html.ValidationMessageFor(model => model.GroupName)%>
    <p>
        <input type="submit" value="Create" style="color:Orange; font-weight:bolder;"/>
    </p>
    </fieldset>
    <%} %>        
  </div>

任何人都可以帮助我在 MVC3 中执行此操作....我必须在下拉菜单中显示 RoleNames 我必须检索在控制器中的 [AcceptVerbs(HttpVerbs.Post)] 中选择的值....请有任何想法

我有一个我的 [AcceptVerbs(HttpVerbs.Post)] 的存储过程,其中使用选定的下拉值我将在 Db 中插入 RoleName 的 RoleID

     Create Procedure InsertEmplyoee
       (
       @EmployeeName varchar(50),
       @EmployeeEmailId varchar(50),
      @EmployeePassword varchar (50),
       @GroupName varchar (50)
          )
       as
       begin
       insert into EmployeeDetails   (EmployeeName,EmployeeEmailId,EmployeePassword,GroupId) values 
    (@EmployeeName,@EmployeeEmailId,@EmployeePassword,(select GroupId from   EmployeeGroup where EmplopyeeRole=@GroupName ))

结束

4

2 回答 2

1

想想你将如何POST将选定的角色 id 返回到服务器。

而不是public static List<ResourceModel> GetList { get; set; }使用 public int RoleId {get;set;}. 您不能@Html.DropDownListFor为 a创建一个List<T>,这没有任何意义,您只是在发回一个值。

接下来在您的模型中创建一个返回IEnumerableof 的方法SelectListItem

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
                                     });
    } 

最后在你看来:

@Html.DropDownListFor(x=> x.RoleId, ResourceModel.GetRoles())

于 2012-07-18T07:24:40.510 回答
0

让你的 Get Action 像这样:

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

GetRoles应该返回一个列表Role

在您看来:

@Html.DropDownList("RoleId")

编辑:解释

Html.DropDownList方法将搜索ViewBagViewData实际上)一个SelectListItem带有名称RoleId的列表并用它创建下拉列表。您无需自己明确指定列表。

编辑:更正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;
}

我一直与EntityFramework. 你为什么不使用那个或其他一些ORM?

于 2012-07-18T07:46:06.570 回答