asp.net mvc - How to rewrite MVC Actions & properties to ASP Core MVVM Razor Pages -


in new release of asp core 2.0 last month, introduced razor pages, has me in loop since old controller & model frpm mvc missing asp core 2 razor pages.

my understanding page default binding of properties using [bindproperty] attribute outside action/method!!!!, because moved mvvm framework opposed mvc framework.

  1. question: while trying rewrite traditional actions, since there no controllers, how move code new razorpages mvvm framework, i.e. and , how bind properties, , actions/handlers?
  2. since properties not in signature, how action/handler know properties passed view/razor page?

what pagemodel?

public class createmodel : pagemodel // pagemodel, new or same old model? {     private readonly appdbcontext _db;      public createmodel(appdbcontext db)     {         _db = db;     }      [bindproperty]     public customer customer { get; set; } // why property outside?      public async task<iactionresult> onpostasync()     {         if (!modelstate.isvalid)         {             return page();         }          _db.customers.add(customer);         await _db.savechangesasync();         return redirecttopage("/index");     } } 

razor pages understanding pretty replacement old asp.net forms have page logic. bit how php things.

if create page, let's pages/index2.cshtml should create (or may created in visual studio) "code-behind" file called pages/index2.cshtml.cs example.

// page file  @page @using razorpages @model indexmodel2  <h2>separate page model</h2> <p>     @model.message </p>   // code-behind file  using microsoft.aspnetcore.mvc.razorpages; using system;  namespace razorpages {     public class indexmodel2 : pagemodel     {         public string message { get; private set; } = "pagemodel in c#";          public void onget()         {             message += $" server time { datetime.now }";         }     } } 

you can still have models , initialize them in code-behind file. if want controllers, suggest do not use razor pages , use classical mvc. can create new project it, don't choose razor pages template. you not need create razor pages project. option. not use since think prone 1 repeat code since every code-behind file valid 1 page afaik.

what pagemodel?

a pagemodel code-behind file server side logic specific page.

i not sure asking for, bind models other razor page , properties in code-behind class. model code-behind file in example.

how action/handler know properties passed view/razor page?

the action handler knows specifying in razor page: <input asp-for="customer.name" />

please read more razor pages here: https://docs.microsoft.com/en-us/aspnet/core/mvc/razor-pages/?tabs=visual-studio


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -