1-) C# MVC - Controllerden View'e veri taşıma
1-) C# MVC - ViewData kullanarak yapmak
* Controller kısmındaki action'umuz aşağıdak gibi
public ActionResult test(int id) // http://localhost:50003/departman/test/2
{
ViewData["id"] = id;
return View();
}
* view'de ise aşağıdaki gibi alırsın
<div>
view datadan gelen veri = @ViewData["id"]
</div>
2-) BU ÇOK ÖNEMLİ
* Controller kısmındaki action'umuz aşağıdak gibi
public ActionResult test(int id) // http://localhost:50003/departman/test/2
{
Departman departman = new Departman { Id = id, Ad = "Bilgi İşlem" };
ViewData["departman"] = departman;
return View();
}
* view'de ise aşağıdaki gibi alırsın
@using PersonelMVC.Models
@{
Layout = null;
Departman departman = (Departman)ViewData["departman"];
}
<!DOCTYPE html>
<html><head>...</head><body>
<div> view datadan gelen veri = @departman.Id </div>
<div> view datadan gelen veri = @departman.Ad </div>
</body></html>
3-) View'e parametre olarak model göndermek
* Controller kısmındaki action'umuz aşağıdak gibi
public ActionResult test(int id) // http://localhost:50003/departman/test/2
{
Departman departman = new Departman { Id = id, Ad = "Bilgi İşlem" };
return View(departman);
}
* view'de ise aşağıdaki gibi alırsın
@model PersonelMVC.Models.Departman
@{
Layout = null;
}
<!DOCTYPE html><html><body>
<div> view datadan gelen veritt = @Model.Id </div>
<div> view datadan gelen veritt = @Model.Ad </div>
</body></html>