Let’s see How to add and retrieve MVC Cookies in ASP.NET MVC Web application.
ASP.NET MVC Cookies Add Retrieve Example:
The application will be asking few questions to the user like favorite colors, number, fruit and email address and store the values in MVC Cookies and the next page will be showing up the values to the users.
Create a new Model class Question.cs
in your MVC Models folder.
Models > Question.cs:
using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Web; namespace MvcApplication1.Models { public class Question { [Required(ErrorMessage = "Please enter your Favourite Fruit.")] [Display(Name = "Favourite Fruit : ")] public string Fruit { get; set; } [Required(ErrorMessage = "Please enter your Favourite Day.")] [Display(Name = "Favourite Day : ")] public string Day { get; set; } [DataType(DataType.EmailAddress)] [Required(ErrorMessage = "Please enter your Email Address.")] [Display(Name = "EmailAddress : ")] public string EmailAddress { get; set; } [Required(ErrorMessage = "Please enter your Favourite Color.")] [Display(Name = "Favourite Color : ")] public string Color { get; set; } public string strFruit { get; set; } public string strDay { get; set; } public string strEmailAddress { get; set; } public string strColor { get; set; } } }
This class will declare all the necessary String variables that will be used in our MVC application.
Create a new Controller with name Questions
and edit it as following :
Controllers > QuestionsController.cs :
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using MvcApplication1.Models; namespace MvcApplication1.Controllers { public class QuestionsController : Controller { // // GET: /Questions/ public ActionResult Index() { return View(); } [HttpGet] public ActionResult Questions() { return View(); } [HttpPost] public ActionResult Questions(Question questions) { if (ModelState.IsValid) { Response.Cookies.Add(new HttpCookie("Day", questions.Day)); Response.Cookies.Add(new HttpCookie("Color", questions.Color)); Response.Cookies.Add(new HttpCookie("EmailAddress", questions.EmailAddress)); Response.Cookies.Add(new HttpCookie("Fruit", questions.Fruit)); //RedirectToAction("MyAnswers"); return RedirectToAction("MyAnswers", "Questions"); } return View(questions); } public ActionResult MyAnswers() { return View(); } } }
Add a View to the Questions Controller method. Right-Click on the Questions method name and Click add view. Add a View with name Questions.cs.html
:
Views > Questions > Questions.cs.html :
@model MvcApplication1.Models.Question @{ ViewBag.Title = "Questions"; Layout = "~/Views/Layout/_webLayout.cshtml"; } <h2>Questions</h2> @using (Html.BeginForm()) { <table width="100%"> <tr> <td align="center"> <span>Your : </span> <table class="table-condensed"> <tr> <td align="left">Favourite Color : </td> <td align="right"> @Html.TextBoxFor(m => m.Color) </td> </tr> <tr> <td align="left">Favourite Day : </td> <td align="right"> @Html.TextBoxFor(m => m.Day) </td> </tr> <tr> <td align="left">EmailAddress : </td> <td align="right"> @Html.TextBoxFor(m => m.EmailAddress) </td> </tr> <tr> <td align="left">Favourite Fruit : </td> <td align="right"> @Html.TextBoxFor(m => m.Fruit) </td> </tr> </table> </td> </tr> </table> <br /> <input value="Okay!" type="submit" /> }
This view will be used to show the Questions to the user. Add another View with name MyAnswers.cshtml
and edit it as below :
Views > Questions > MyAnswers.cshtml :
@model MvcApplication1.Models.User @{ ViewBag.Title = "UserHome"; Layout = "~/Views/Layout/_webLayout.cshtml"; } <h2>My Homepage</h2> <span style="color: @Request.Cookies["Color"].Value.ToString(); font-size: 20px;">You Entered : <br /> Day : <b>@Request.Cookies["Day"].Value.ToString()</b><br /> Fruit : <b>@Request.Cookies["Fruit"].Value.ToString()</b><br /> Email Address : <b>@Request.Cookies["EmailAddress"].Value.ToString()</b><br /> Color : <b>@Request.Cookies["Color"].Value.ToString()</b><br /> </span>
This view will be used to show the enter values to the Users which will be retrieved from MVC Cookies.
Output :
Thank You.
Also see :
Creating MVC Login Application with MS SQL Database and Razor.
How to upload files in ASP.NET MVC and save in Database.
Binding Angular JS Bind HTML table from SQL in ASP.NET MVC C#
Bind|Populate ASP.NET MVC C# Dropdownlist from SQL
C# ASP.NET MVC Add and Retrieve Cookies
Creating a Login Page in ASP.NET MVC C# using SQL table and Razor
Creating a Registration page in ASP.NET C#