Hi friends,in this article i will explain about How to use sessions in MVC 4 Razor application?.
I already explained in the previous articles about MVC4 Razor:How to retrieve view values on POST method in Controller using FormCollection,Mvc 4 Razor CRUD Operation using C# and How to create a dropdown list field for MVC4 Razor view?
Controller AccountController.cs
I already explained in the previous articles about MVC4 Razor:How to retrieve view values on POST method in Controller using FormCollection,Mvc 4 Razor CRUD Operation using C# and How to create a dropdown list field for MVC4 Razor view?
Model Account.cs:
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace MvcCRUD.Models
{
public
class Account
{
public string
firstName { get; set;
}
public string
lastname { get; set;
}
public string
address { get; set;
}
SqlConnection con = new
SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);
public int Login()
{
SqlCommand cmd = new
SqlCommand("Login_Student",
con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@firstname",
firstName);
cmd.Parameters.AddWithValue("@lastname",
lastname);
SqlParameter id = new
SqlParameter("@retval",
SqlDbType.Int);
id.Direction = ParameterDirection.Output;
cmd.Parameters.Add(id);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
int retval = Convert.ToInt32(cmd.Parameters["@retval"].Value);
return retval;
}
}
}
|
Controller AccountController.cs
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcCRUD.Models;
using
System.Web.SessionState;
namespace MvcCRUD.Controllers
{
public
class AccountController
: Controller
{
public ActionResult
Login()
{
return View();
}
[HttpPost]
public ActionResult
Login(FormCollection frm)
{
Account obj = new
Account();
obj.firstName =
frm[0].ToString();
obj.lastname =
frm[1].ToString();
int retval =Convert.ToInt32(obj.Login());
if (retval == 1)
{
Session["User"] = frm[0].ToString();
return Redirect("~/Student/index");
}
else
{
return View("Register");
}
}
public ActionResult
Index()
{
ViewBag.User ="Welcome " + Session["User"];
return View();
}
}
}
|
Add View Index.cshtml
@model MvcCRUD.Models.Account
@{
ViewBag.Title = "Index";
}
<h2>@ViewBag.User</h2>
|
The output will be as shown in the
below figure.
NEXT CHAPTER >>
"If you like my blog or articles, you can appreciate by leaving your comments or Liking my Facebook pageAspdotnet-kishore, following on Google+ Aspdotnet-Kishore, Twitter on AspdotnetKishore, Linked in Aspdotnet-Kishore, stumbling my posts on stumble upon and subscribing on RSSfeed Aspdotnet-Kishore for free updates directly to your Email inbox . Watch my blog for more articles."
"If you like my blog or articles, you can appreciate by leaving your comments or Liking my Facebook pageAspdotnet-kishore, following on Google+ Aspdotnet-Kishore, Twitter on AspdotnetKishore, Linked in Aspdotnet-Kishore, stumbling my posts on stumble upon and subscribing on RSSfeed Aspdotnet-Kishore for free updates directly to your Email inbox . Watch my blog for more articles."
No comments:
Post a Comment