You are on page 1of 13

http://phantienquang.blogspot.com/2014/09/mvc5-series-p4-chia-se-du-lieu.

html

[MVC5 SERIES - P4] CHIA S D LIU

Chia s d liu L cch mt i tng d liu thnh phn ny c s dng


thnh phn khc ti cng thi im hoc khc thi im. Trong m hnh MVC, d liu
c chia s theo cc cch sau:
1.

T Controller sang View: ViewBag, ViewData, Model

2.

Phin lm vic: Session

3.

Ton ng dng web: Application

4.

Trn 1 client: Cookie


1. VIEWBAG, VIEWDATA

y l cch n gin nht truyn d liu t Controller sang View.


The ViewBag object is a wrapper around the ViewData object that allows you to create
dynamic properties for the ViewBag. Both the ViewData and ViewBag objects are great
for accessing extra data (i.e., outside the data model), between the controller and view.
ViewBag, ViewData usage

Hiu n gin, ViewBag l mt kiu d liu ng, bn c th b bt c th g vo ,


vi bt c kiu d liu g.
Cch dng
Trong Controller
ViewBag.XXX
Trong View
@ViewBag.XXX

V d

ViewData hon ton tng t vi ViewBag, ch khc cch vit


Trong Controller
ViewData["Hello"] = "Hello World";
Trong View
@ViewData["Hello"] @ViewData.Hello

2. MODEL

Model l mt object bt k. Model c truyn t Controller sang View thng qua lnh
return View(model);
V d

Theo nh trn, truy xut thng tin c truyn t Controller ta s dng @Model,
y chnh l i tng c truyn t Controller thng qua lnh return View(model);
Ch
khi vit @Model. s khng c ch IntelliSense bi v View ch bit tip nhn Model t
Controller ch khng bit Model thuc kiu g. Do ta phi g chnh xc thuc tnh
ca Model. s dng c chc nng IntelliSense, ta ch nh cho View bit Model
c kiu l g bng cu lnh sau

V mt ch quan trng na, l khi ch nh kiu cho Model th ta dng c


php@model (m nh), cn khi s dng ta dng @Model (M hoa).
Model vi tp hp

i vi mt tp hp, cch s dng hon ton tng t. Tuy nhin cch ch nh model
v cch s dng c i cht khc. Khi trong View, @Model c xem nh 1 tp
hp, do ta phi dng 1 vng lp ly d liu t model.

3. SESSION

Session l mt vng nh m server cp pht cho user lu d liu ring ca mnh.


Vng nh ny s c gii phng khi ht phin lm vic (Refresh trang, ng trnh
duyt...)
Ty thuc vo ni s dng Session s c 1 cch vit khc nhau
Trong Controller
Session["Demo"] = "This is demo";
Trong View
@Session["Demo"]
Trong 1 class bt k khc
HttpContext.Current.Session["Demo"] = "This is demo";
Mt s thao tc c bn vi Session

ADD(KEY, VALUE)
Thm mi 1 i tng vo Session

Session.Add("Name","PhanTienQuang");

[KEY] = VALUE
Thm mi (nu cha c) hoc thay th (nu c) mt i tng trong Session (thng dng)

Session["Name"] = "TienQuang";

REMOVE(KEY)
Xa i tng ra khi Session

Session.Remove("Name")

CLEAR()
Xa tt c cc i tng khi Session

Session.Clear()

ABANDON()
Hy Session

Session.Abandon()

SESSIONID
Ly id ca Session hin ti

var id = Session.SessionId;
V d demo: Khi nhn Submit, nhng thng tin nhp vo s c lu vo Session v
chuyn qua trang hin th. Trong trang Profile, nu Logout th xa Session.

Bc 1 thm 1 Controller v Model

Tip, thm giao din ng k

Tip theo, khi nhn nt Submit, d liu trong form s c chuyn n Action Register
trong Controller Student vi Method l Post. Ta thm Action Register cho
StudentController nh sau:

Action Register chuyn n Action Profile

Trong Action Profile kim tra xem c Session Student cha, nu cha c th n s
quay li trang ng k. Do , nu ban u cha c g m ta nhp url
localhost.../Student/Profile th vn s trang ng k m thi.
Cui cng l x l khi nhn vo Logout. Logout s chuyn n Action Logout v xa
Session["Student"] ng thi quay li trang ng k.

public ActionResult Logout()


{
// delete session
Session.Remove("Student");

return View("Index");
}
view rawactionresultlogout.cs hosted with by GitHub

Hy vng qua v d nh ny, bn s nm c cch thc s dng Session. y l mt


k thut quan trng v thng dng trong web thng mi nh duy tr gi hng, cc
mt hng xem...
4. APPLICATION

Application l ni s dng duy tr d liu c chia s trn ton ng dng. Bt c


ai cng c th thc hin cc thao tc truy xut, thay i, xa d liu trn Application.
Cch thc ly i tng Application cng tng t so vi Session, ty thuc vo code
c vit u th c 1 cch ly khc nhau

CONTROLLER
HttpContext.Application
HttpContext.Application["Hello"] = "Hello World";

VIEW
@AppState
@AppState["Hello"]

CLASS BT K
HttpContext.Current.Application
HttpContext.Current.Application["Hello"] = "Hello World";
Cc thao tc vi Application

ADD(KEY, VALUE)
Thm mi 1 i tng vo Application

HttpContext.Application.Add("Visitor", 0);

[KEY] = VALUE
Thm mi hoc thay th 1 i tng

HttpContext.Application["Visitor"] = 0;

REMOVE(KEY)
Xa i tng

HttpContext.Application.Remove("Visitor");

CLEAR()
Xa sch cc i tng trong Application

HttpContext.Application.Clear()

LOCK()
Kha Application, cc yu cu ca user khc phi i unlock

HttpContext.Application.Lock()

UNLOCK()
M kha Application

HttpContext.Application.UnLock();
Nh bng trn, cc thao tc trn Application hon ton tng t vi Session, ch thm
2 phng thc Lock() v UnLock(). Bi v Application c chia s cho nhiu user
cng lc, do c th xy ra hin tng xung t d liu, v th m 2 phng thc ny
c b xung x l vn xung t .
NG DNG CA APPLICATION

Mt trong nhng ng dng n gin nhng hiu qu nht ca Application chnh l m


s ln khch truy cp trang web.

5. COOKIE

Cookie l nhng mu tin nh c lu tr trn my tnh ca client. Cookie c truyn


thng vi Server thng qua Request v Response.
Hn ch ca Cookie l ch lu tr d liu text v kch thc ti a l 4KB. V vic lu
tr Cookie cn ty thuc vo ngi dng c cho php lu tr hay khng.
Thao tc vi Cookie bao gm nhn + x l Cookie t client v To + gi Cookie v
Server.
NHN COOKIE T CLIENT

CONTROLLER
Request.Cookies
Request.Cookies["id"];

VIEW
@Request.Cookies
@Request.Cookies["id"]

MODEL
HttpContext.Current.Request.Cookies
HttpContext.Current.Request.Cookies["id"];
TO MI COOKIE
var myCookie = new HttpCookie("id", "ABC123");

Request.Cookies.Add(myCookie);
view rawcreatecookie.cs hosted with by GitHub

CC THUC TNH V PHNG THC CA COOKIE

VALUE
Gi tr ca Cookie

myCookie.Value = "ABCD123";

NAME
Tn ca Cookie

myCookie.Name = "ids";

EXPIRES
Thi hn tn ti ca Cookie

myCookie.Expires = DateTime.Now.AddDays(1);

VALUES.ADD(KEY, VALUE)
Thm 1 gi tr

myCookie.Values.Add("id2", "DEF345");

VALUES[KEY] = VALUE
Thm mi hoc thay th 1 gi tr

myCookie.Values["id"] = "ABCDEF";
Ch : xa 1 cookie

myCookie.Expires = DateTime.Now.AddDays(-1);

You might also like