Bind vs BindProperty vs BindProperties

Bind vs BindProperty vs BindProperties

Model Binding

Bind :

Örneğin :

aşağıdaki gibi bir modelimiz olsun

public class OrnekModel
    {
        public string Id { get; set; }
        public string Name { get; set; }
    }

ve aşağıdaki gibi bi action'ımız:

  public IActionResult Index([Bind("Id")]OrnekModel ornekModel)
        {
            ViewBag.Id = ornekModel.Id;
            ViewBag.Name = ornekModel.Name;
            return View();
        }

index.cshtml aşağıdaki gibi olsun :

@ViewBag.Id 
@ViewBag.Name

ve biz url'ye şunu yazdığımızda :

https://localhost:44393/?Id=xyz&Name=abc

xyz

sonucunu görürüz çünkü bind ile sadece id'yi action'a bağladık.

BindProperty:

Örneğin :

public class HomeController : Controller
    {

        [BindProperty(SupportsGet = true)]
        public int Id { get; set; }

        public IActionResult Index()
        {
            ViewBag.Id = Id;
            return View();
        }
.
.
.
.

Index.cshtlm :

@ViewBag.Id

sonra Url'ye aşağıdakini yazdığımızda :

https://localhost:44393/?Name=1234

View'da ilgili kısımda :

1234

sayısını görürüz.

BindProperties :

Örneğin :

    [BindProperties(SupportsGet = true)]
    public class HomeController : Controller
    {
        public string Name { get; set; }

        public IActionResult Index()
        {
            ViewBag.Name = Name;
            return View();
        }
        .
        .  
        .  
        .  
        .

daha sonra url'ye şunu yazdığımızda :

https://localhost:44393/?Name=MustafaSamedYeyin

eğer view'de de aşağıdaki kod varsa

@ViewBag.Name

view'in ilgili yerinde

MustafaSamedYeyin

yazısını görürüz.