Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hey m just new to mVC want to create a login page for my application.i can do the simple one. but i want to see a error messege there when putting wrong username and password like user not found


here is my controller code

C#
using MvcApplication1.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace MvcApplication1.Controllers
{
    public class LoginController : Controller
    {
        //
        // GET: /Login/

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Login()
        {
            return View();
        }

       [HttpPost]
 public ActionResult Login(Customer  d)
{
    using (CustomerDataEntities oe = new CustomerDataEntities())
    {
        var user = oe.Customers.Where(a => a.UserName.Equals(d.UserName) && a.Password.Equals(d.Password)).FirstOrDefault();
        return RedirectToAction("Index","Home");
    }
           
}
        }

    }
Posted
Updated 9-Aug-16 4:59am
Comments
F-ES Sitecore 3-Dec-15 4:47am    
If the login fails then you need to return the View as normal, passing the submitted data as the model so that the username (and if they have checked "Remember me" etc) remains on the page, and also an error message to show (or a bool on the model to show there is an error). This is basic MVC 101, go through MVC tutorials and these kind of techniques will be shown, you can't learn a technology like MVC from scratch from forum posts. Google "mvc music store" for a good start-up tutorial.

FirstOrDefault method will return null as default value if UserName and Password does not match in Customer table. So you can use "if" condition to verify user is null or not.
Sample code shown below:
public ActionResult Login(Customer d)
        {
            using (CustomerDataEntities oe = new CustomerDataEntities())
            {
                var user = oe.Customers.Where(a => a.UserName.Equals(d.UserName) &&
                                                  a.Password.Equals(d.Password)).FirstOrDefault();
                if (user == null)
                {
                    // If UserName and Password is incorrect then redirect to Login page
                    return RedirectToAction("Login", "Home");
                }
                else
                {
                    // If UserName and Password is correct then redirect to Index page
                    return RedirectToAction("Index", "Home");
                }
            }
        }

In case of any issue let us know.
 
Share this answer
 
Comments
Member 11897361 8-Dec-15 8:35am    
hey m having one issue here, m getting one silly error saying that
"The current request for action 'Login' on controller type 'LoginController' is ambiguous between the following action methods:
System.Web.Mvc.ActionResult Login() on type MvcApplication1.Controllers.LoginController
System.Web.Mvc.ActionResult login(MvcApplication1.Models.Customer) on type MvcApplication1.Controllers.LoginController"
What does that mean where m making mistake
Snesh Prajapati 8-Dec-15 11:32am    
The error is because of same method name. You have to decorate first one with [HttpGet] where no parameter is given and [HttpPost] where u are passing customer.
Or if both are HttpGet only then use ActionName attribute.
Or some trick as described at http://stackoverflow.com/questions/9552761/get-and-post-methods-with-the-same-action-name-in-the-same-controller
Member 11897361 8-Dec-15 19:56pm    
using MvcApplication1.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace MvcApplication1.Controllers
{
public class LoginController : Controller
{
//
// GET: /Login/

public ActionResult Index()
{
return View();
}
public ActionResult Login()
{
return View();
}

public ActionResult Login(Customer d)
{
using (CustomerDataEntities oe = new CustomerDataEntities())
{
var user = oe.Customers.FirstOrDefault(a => a.UserName.Equals(d.UserName) && a.Password.Equals(d.Password));
if (user == null)
{
TempData["ErrorMessage"] = "Invalid user name or password.";
return RedirectToAction("Login", "Home");
}
else
{
return RedirectToAction("Index", "Home");
}
}
}
}
}




here is my code can you just help me. m just confused here.Sorry
Member 11897361 8-Dec-15 20:00pm    
ohhh yeyyyy its working i just used the http get and post methods,thnak you so much
Snesh Prajapati 8-Dec-15 21:26pm    
Most Welcome !!
Controller-
C#
public ActionResult Login(Customer d)
        {
            using (CustomerDataEntities oe = new CustomerDataEntities())
            {
                var user = oe.Customers.FirstOrDefault(a => a.UserName.Equals(d.UserName) && a.Password.Equals(d.Password));
                if (user == null)
                {
                    TempData["ErrorMessage"] = "Invalid user name or password."
                    return RedirectToAction("Login", "Home");
                }
                else
                {
                    return RedirectToAction("Index", "Home");
                }
            }
        }

View- Place this code wherever you want to display the error message
ASP
@{
    if(TempData["ErrorMessage"] != null)
    {
        <div id="errorMsg" style="border: solid 1px red; padding: 50px 10px; color:red">
            @TempData["ErrorMessage"]
        </div>
    }
}


-KR
 
Share this answer
 
Comments
Member 11897361 8-Dec-15 20:01pm    
thank you so much your code is working .Thanks for your time

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900