Professional Documents
Culture Documents
Truyendulieu 1
Truyendulieu 1
html
2.
3.
4.
V d
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
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
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.
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:
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.
return View("Index");
}
view rawactionresultlogout.cs hosted with by GitHub
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
5. COOKIE
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
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);