Skip to content

Code for registration in asp.net mvc application

sudheer kumar

sudheer kumar

Asked Aug 1, 2014, 4:34 AM EDT2 answers
Share
LinkedInX
how we can register the detail through asp.net mvc application and how we can fetch the detail from server using mvc in asp.net

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    These examples are out-of-box from Visual Studio.

    Goto Visual Studio->new Project->Visual C#-> web->asp.net mvc 4 web application -> choose internet or intranet
    0
  • bhargav batchu
    0
    PowerShell
    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Routing;
    using System.Web.Security;
    using System.Web.SessionState;
    using System.IO;
    using view.DataResources;
    using view.Models;
    
    
    namespace view.Controllers
    {
        public class HomeController : Controller
        {
            public ActionResult Index()
            {
                return View();
            }
            [OutputCacheAttribute(VaryByParam = "*", Duration = 0, NoStore = true)]
            [HttpGet]
    Java
            public ActionResult Login()
            {
               if (Session["LoginStatus"] != null)
                {
                    LogonModel log = new LogonModel();
                    return RedirectToAction("LoginHomePage", "Home", new { EmpId = Session["id"]});
                }
                else
                {
                    return View();
                }
            }
            [HttpPost]
            public ActionResult Login(FormCollection form)
            {
                    LogonModel log = new LogonModel();
                    log.EmpId = Convert.ToInt32(form["EmpId"]);
                    log.EmpPassword = form["EmpPassword"];
                    DataAccess da = new DataAccess();
                    if (da.LoginAuthentication(log))
                    {
                        Session["LoginStatus"] = "true";
    
                        return RedirectToAction("LoginHomePage", "Home", new { EmpId = form["EmpId"] });
                    }
                    else
                    {
                        ViewBag.Msg = "Login faild plz enter correct id and password";
                        return View();
                    }
            }
            [OutputCacheAttribute(VaryByParam = "*", Duration = 0, NoStore = true)]
    Java
            public ActionResult LoginHomePage(int EmpId)
            {
                if (Session["LoginStatus"] != null)
                {
                    try
                    {
                        LoginModel ed = new LoginModel();
                        Session["id"] = Convert.ToInt32(EmpId);
                        ed.EmpId = Convert.ToInt32(EmpId);
                        DataAccess da = new DataAccess();
                        //DataTable dt = da.GetTableData();
                        DataTable dt = da.GetDataById(ed);
                        ed.EmpName = Convert.ToString(dt.Rows[0]["EmpName"]);
                        ed.EmpPassword = Convert.ToString(dt.Rows[0]["EmpPassword"]);
                        ed.EmpConformPassword = Convert.ToString(dt.Rows[0]["EmpConformPassword"]);
                        ed.EmpSalary = Convert.ToString(dt.Rows[0]["EmpSalary"]);
                        ed.EmpDesignation = Convert.ToString(dt.Rows[0]["EmpDesignation"]);
                        return View(ed);
                    }
                    catch
                    {
                        ViewBag.msg = "Employeeid deleted";
                        Session["LoginStatus"] = null;
                        Session["id"] = null;
                        return View("Login");
                    }
                }
    
    
                else
                {
                    return RedirectToAction("Login");
                }
            }
            public ActionResult LogOff()
            {
                Session["LoginStatus"] = null;
                Session.Abandon();
                return RedirectToAction("Login", "Home");
            }
            [HttpGet]
            public ActionResult Register()
            {
                List<SelectListItem> li = new List<SelectListItem>();
                li.Add(new SelectListItem { Text = "Ceo", Value = "ceo" });
                li.Add(new SelectListItem { Text = "Hr", Value = "hr" });
                li.Add(new SelectListItem { Text = "manager", Value = "manager" });
                li.Add(new SelectListItem { Text = "salesman", Value = "salesman" });
                ViewData["EmpDesignation"] = li;
                RegisterModel reg = new RegisterModel();
                reg.EmpDesignationDropDown = li;
                return View(reg);
            }
            [HttpPost]
    Java
            public ActionResult Register(FormCollection form)
            {
                RegisterModel reg = new RegisterModel();
                reg.EmpId = Convert.ToInt32(form["EmpId"]);
                reg.EmpName = form["EmpName"];
                reg.EmpPassword = form["EmpPassword"];
                reg.EmpConformPassword = form["EmpConformPassword"];
                reg.EmpSalary = form["EmpSalary"];
                reg.EmpDesignation = form["EmpDesignation"];
                reg.Gender = form["Gender"];
                List<SelectListItem> li = new List<SelectListItem>();
                li.Add(new SelectListItem { Text = "Ceo", Value = "ceo" });
                li.Add(new SelectListItem { Text = "Hr", Value = "hr" });
                li.Add(new SelectListItem { Text = "manager", Value = "manager" });
                li.Add(new SelectListItem { Text = "salesman",Value = "salesman" });
                reg.EmpDesignationDropDown = li;
                DataAccess da = new DataAccess();
                //da.FillTable(reg);
                if (da.FillTable(reg))
                {
                    ViewBag.msg = " Registerd  successfully";
                }
                else
                {
                    ViewBag.msg = "EmployeeId Already Registerd in the Record.plz give an another EmpolyeeId number";
                }
                    return View(reg);
            }
            [OutputCacheAttribute(VaryByParam = "*", Duration = 0, NoStore = true)]
            public ActionResult Details()
            {
                if (Session["LoginStatus"] != null)
                {
                    DataAccess da = new DataAccess();
                    DataTable dt = da.GetTableData();
                    List<RegisterModel> grid = new List<RegisterModel>();
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        grid.Add(new RegisterModel() { EmpId = Convert.ToInt32(dt.Rows[i]["EmpId"]), EmpName = Convert.ToString(dt.Rows[i]["EmpName"]), EmpDesignation = Convert.ToString(dt.Rows[i]["EmpDesignation"]), EmpSalary = Convert.ToString(dt.Rows[i]["EmpSalary"]) });
                    }
                    return View(grid);
                }
                else
                {
                    return RedirectToAction("Login", "Home");
                }
            }
            [OutputCacheAttribute(VaryByParam = "*", Duration = 0, NoStore = true)]
    Java
            public ActionResult Editemp()
            {
                if (Session["LoginStatus"] != null)
                {
                DataAccess da = new DataAccess();
                DataTable dt = da.GetTableData();
                List<RegisterModel> grid = new List<RegisterModel>();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    grid.Add(new RegisterModel() { EmpId = Convert.ToInt32(dt.Rows[i]["EmpId"]), EmpName = Convert.ToString(dt.Rows[i]["EmpName"]), EmpDesignation = Convert.ToString(dt.Rows[i]["EmpDesignation"]), EmpSalary = Convert.ToString(dt.Rows[i]["EmpSalary"]) });
                }
                return View(grid);
                }
                else
                {
                    return RedirectToAction("Login", "Home");
                }
             }
            [HttpGet]
            [OutputCacheAttribute(VaryByParam = "*", Duration = 0, NoStore = true)]
    Java
            public ActionResult Edit(int EmpId)
            {
                if (Session["LoginStatus"] != null)
                {
                    EditModel ed = new EditModel();
                    ed.EmpId = Convert.ToInt32(EmpId);
                    DataAccess da = new DataAccess();
                    DataTable dt = da.GetDataById(ed);
                    ed.EmpName = Convert.ToString(dt.Rows[0]["EmpName"]);
                    ed.EmpPassword = Convert.ToString(dt.Rows[0]["EmpPassword"]);
                    ed.EmpConformPassword = Convert.ToString(dt.Rows[0]["EmpConformPassword"]);
                    ed.EmpSalary = Convert.ToString(dt.Rows[0]["EmpSalary"]);
                    ed.EmpDesignation = Convert.ToString(dt.Rows[0]["EmpDesignation"]);
                    List<SelectListItem> li = new List<SelectListItem>();
                    li.Add(new SelectListItem { Text = "Ceo", Value = "ceo" });
                    li.Add(new SelectListItem { Text = "Hr", Value = "hr" });
                    li.Add(new SelectListItem { Text = "manager", Value = "manager"});
                    li.Add(new SelectListItem { Text = "salesman", Value = "salesman" });
                    li.Where(m => m.Value == ed.EmpDesignation).FirstOrDefault().Selected = true;
                    ed.EmpDesignationDropDown = li;
                    return View(ed);
                }
                else
                {
                    return RedirectToAction("Login", "Home");
                }
              }
            [HttpPost]
            public ActionResult Edit(FormCollection form1)
            {
                if (Session["LoginStatus"] != null)
                {
                    EditModel ed = new EditModel();
                    ed.EmpId = Convert.ToInt32(form1["EmpId"]);
                    ed.EmpName = Convert.ToString(form1["EmpName"]);
                    ed.EmpPassword = Convert.ToString(form1["EmpPassword"]);
                    ed.EmpConformPassword = Convert.ToString(form1["EmpConformPassword"]);
                    ed.EmpSalary = Convert.ToString(form1["EmpSalary"]);
                    ed.EmpDesignation = Convert.ToString(form1["EmpDesignation"]);
                    DataAccess da = new DataAccess();
                    da.FillTable(ed);
                    ViewBag.ms = "Edited successfully";
                    return RedirectToAction("Details", "Home");
                }
                else
                {
                    return RedirectToAction("Login","Home");
                }
             }
            [OutputCacheAttribute(VaryByParam = "*", Duration = 0, NoStore = true)]
    C#
            public ActionResult Delete(int EmpId)
             {
                 if (Session["LoginStatus"] != null)
               {
                 DeleteModel de = new DeleteModel();
                 de.EmpId = Convert.ToInt32(EmpId);
                 DataAccess da = new DataAccess();
                 da.FillTable(de);
                 return RedirectToAction("EditEmp","Home");
               }
                 else
               {
                     return RedirectToAction("Login", "Home");
               }
            }
            //public ActionResult FileUpload(HttpPostedFileBase file)
            [HttpPost]
            public ActionResult FileUpload(HttpPostedFileBase file)
            {
            try
            {
                if (file.ContentLength > 0)
            {
                var fileName = Path.GetFileName(file.FileName);
                var path = Path.Combine(Server.MapPath("~/img"), fileName);
                file.SaveAs(path);
            }
                ViewBag.Message = "Upload successful";
                return RedirectToAction("Index");
            }
            catch
            {
                ViewBag.Message = "Upload failed";
                return RedirectToAction("Uploads"); } }
            }
     }

    And connect to data base write some store procedure 
    0