sample mvc4 login application

Posted by Parthibansk under ASP.NET MVC on 6/10/2015 | Points: 10 | Views : 761 | Status : [Member] | Replies : 3
hi,

i am new to mvc..i need a sample registration and login application in mvc4...




Responses

Posted by: Jayakumars on: 6/10/2015 [Member] [MVP] Bronze | Points: 25

Up
0
Down
hi
try this code

[HttpPost]
public ActionResult form(Registration_Table r,string Command)
{
if (ModelState.IsValid)
{
if (Command == "Login")
{
using (MyDatabaseEntities dc = new MyDatabaseEntities())
{
var v = dc.Registration_Table.Where(a => a.UserName.Equals(r.UserName) && a.Password.Equals(r.Password)).FirstOrDefault();
if (v != null)
{
Session["LogedUserName"] = v.UserName.ToString();
return RedirectToAction("LoginSucess");
}
else
{
return RedirectToAction("form");
}
}
}
else
{
using (MyDatabaseEntities dc = new MyDatabaseEntities())
{
dc.Registration_Table.Add(r);
dc.SaveChanges();
ModelState.Clear();
r = null;
ViewBag.Message = "Success!";
}
}
}
else
{
ViewBag.Message = "Failed!";
}
return View(r);
}

Mark as Answer if its helpful to you

Parthibansk, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Parthibansk on: 6/10/2015 [Member] Starter | Points: 25

Up
0
Down
could you send me login mvc app with code...
checked with all resource but ending in vain..

Parthibansk, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jayakumars on: 6/10/2015 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Hi
This is Controller

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcRegistration.Controllers
{
public class RegistrationController : Controller
{
//
// GET: /Registration/

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


[HttpGet]
public ActionResult form()
{
return View();
}

[HttpPost]
public ActionResult form(Registration_Table r,string Command)
{
if (ModelState.IsValid)
{
if (Command == "Login")
{
using (MyDatabaseEntities dc = new MyDatabaseEntities())
{
var v = dc.Registration_Table.Where(a => a.UserName.Equals(r.UserName) && a.Password.Equals(r.Password)).FirstOrDefault();
if (v != null)
{
Session["LogedUserName"] = v.UserName.ToString();
return RedirectToAction("LoginSucess");
}
else
{
return RedirectToAction("form");
}
}
}
else
{
using (MyDatabaseEntities dc = new MyDatabaseEntities())
{
dc.Registration_Table.Add(r);
dc.SaveChanges();
ModelState.Clear();
r = null;
ViewBag.Message = "Success!";
}
}
}
else
{
ViewBag.Message = "Failed!";
}
return View(r);
}



[HttpGet]
public ActionResult LoginSucess()
{
if (Session["LogedUserName"] != null)
{
return View();
}
else
{
return RedirectToAction("LoginSucess");
}
}

}
}


This is View

@model MvcRegistration.Registration_Table

@{
ViewBag.Title = "form";
}

<h2>form</h2>

@using (Html.BeginForm()) {
@Html.ValidationSummary(true)

<fieldset>
<legend>Registration_Table</legend>

<div class="editor-label">
@Html.LabelFor(model => model.UserName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.UserName)
@Html.ValidationMessageFor(model => model.UserName)
</div>

<div class="editor-label">
@Html.LabelFor(model => model.Password)
</div>
<div class="editor-field">
@*@Html.EditorFor(model => model.Password)*@
@Html.PasswordFor(model => model.Password)
@Html.ValidationMessageFor(model => model.Password)
</div>

<p>
<input type="submit" value="Registration" name="Command" />
<input type="submit" value="Login" name="Command"/>
</p>
</fieldset>
}

<div>
@Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}



Mark as Answer if its helpful to you

Parthibansk, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response