Authentication is getting done from sql, but the error message is getting redirected to different action. i am not able to perform authentication for this on the same page, as i am using user id in dropdown and password in textbox
public class HomeController : Controller
{
public ActionResult Index()
{
SectionLogin Sl = new SectionLogin();
Sl.UserList = GetUser();
return View(Sl);
}
[HttpPost]
public ActionResult Login(SectionLogin Ls)
{
Ls.UserList = GetUser();
using (LoginEntities dbu = new LoginEntities())
{
//var db = Ls.UserList.Where(model => model.Text == Ls.Section_name.ToString() && model.Value == Ls.Section_pwd).FirstOrDefault();
var db = dbu.SectionLogins.Where(model => model.Section_name == Ls.Section_name && model.Section_pwd == Ls.Section_pwd).FirstOrDefault();
if (db == null)
{
TempData["Message"] = "Wrong User name or pwd";
//return View("Index");
}
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 Section_name from [CPSEdb].[dbo].[SectionLogin]";
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["Section_name"].ToString(),
Value = rd["Section_name"].ToString()
});
}
}
con.Close();
}
}
return List;
}
}
@model CPSEDetail.Models.SectionLogin
@{
ViewBag.Title = "Index";
}
@using (Html.BeginForm("Login", "Home", FormMethod.Post))
{
@Html.DropDownListFor(model => model.Section_name, Model.UserList, "--Select")
@Html.TextBoxFor(model => model.Section_pwd)
<input type="submit" value="submit" />
}
@{
ViewBag.Title = "Login";
}
@if (TempData["Message"] != null)
{
<p>@TempData["Message"]</p>
}
<h2>Login</h2>
namespace CPSEDetail.Models
{
using System;
using System.Collections.Generic;
using System.Web.Mvc;
public partial class SectionLogin
{
public string Section_name { get; set; }
public string Section_pwd { get; set; }
public List<SelectListItem> UserList {get;set;}
}
}