Hi sunnyk21,
I have checked your code. Its working correctly.
Check this example.
Model
public class SectionLogin
{
public string Section_name { get; set; }
public string Section_pwd { get; set; }
public List<SelectListItem> UserList { get; set; }
}
Namespaces
using System.Configuration;
using System.Data.SqlClient;
Controller
public class HomeController : Controller
{
// GET: /Home/
public ActionResult Index()
{
SectionLogin Sl = new SectionLogin();
Sl.UserList = GetUser();
return View(Sl);
}
[HttpPost]
public ActionResult Section(SectionLogin Ls)
{
Ls.UserList = GetUser();
using (LoginEntities dbu = new LoginEntities())
{
var db = dbu.Users.Where(model => model.UserName == Ls.Section_name && model.Password == Ls.Section_pwd).FirstOrDefault();
if (db == null)
{
TempData["Message"] = "Wrong User name or pwd";
}
else
{
TempData["Message"] = "Successfull";
}
}
return View();
}
private static List<SelectListItem> GetUser()
{
string str = ConfigurationManager.ConnectionStrings["Con"].ConnectionString;
List<SelectListItem> List = new List<SelectListItem>();
using (SqlConnection con = new SqlConnection(str))
{
string query = "select UserName from [User]";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
con.Open();
using (SqlDataReader rd = cmd.ExecuteReader())
{
while (rd.Read())
{
List.Add(new SelectListItem
{
Text = rd["UserName"].ToString(),
Value = rd["UserName"].ToString()
});
}
}
con.Close();
}
}
return List;
}
}
View
Index
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<_855589_Login_DropDownList.Models.SectionLogin>" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Index</title>
</head>
<body>
<%using (Html.BeginForm("Section", "Home", FormMethod.Post))
{%>
<%:Html.DropDownListFor(model => model.Section_name,Model.UserList, "--Select Section--")%>
<%:Html.TextBoxFor(model => model.Section_pwd) %>
<input type="submit" value="submit" />
<%} %>
</body>
</html>
Section
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Section</title>
</head>
<body>
<div>
<%if (TempData["Message"] != null)
{ %>
<p><%= TempData["Message"]%></p>
<%} %>
</div>
</body>
</html>
Screenshot
