Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Steve Mak
Steve Mak

Posted on • Edited on

     

Cheatsheet for Razor Page

Keywords

View

ViewData|ViewBag|TempData|@page|@model@addTagHelper|@removeTagHelper|@Html.AntiForgeryToken()RenderBody|RenderSectionHtml.BeginForm("<Action>","<Controller>")Html.RenderPartialAsync("<Name of file with extension .cshtml>")Html.DisplayNameFor(model=>model.ReleaseDate)Html.DisplayFor(model=>model.Title)
Enter fullscreen modeExit fullscreen mode

Model

ViewData|ViewBag|TempData|View()|Page()IActionResult|ModelState|RedirectToPage
Enter fullscreen modeExit fullscreen mode

Data Annotation

Reference:https://dev.to/ssmak/test-2fl0

[BindProperty]|[BindProperty(Name="Name123")]|[BindProperty(SupportsGet=true)][Key]|[DisplayName]|[Column]|[StringLength],[Range],[RegularExpression][HttpPost]|[ValidateAntiForgeryToken]
Enter fullscreen modeExit fullscreen mode

Tag Helper

Reference:https://docs.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/built-in/?view=aspnetcore-3.1

asp-page|asp-page-handler|asp-route-<>|asp-action|asp-controllerasp-for|asp-items/*Use with <div>e.g.<div asp-validation-summary="All">    <span>Please correct the following errors</span></div>*/asp-validation-summary/*Use with <span>e.g<span asp-validation-for="FirstName"></span>*/asp-validation-for/*Import partial view*/<partialname="_MenuPartial"/>/*Import view component*/<vc:priority-listmax-priority="2"is-done="false"></vc:priority-list>
Enter fullscreen modeExit fullscreen mode

Html Helper

Html.DisplayNameFor|Html.DisplayFor|Html.RenderPartialAsyncHtml.Partial
Enter fullscreen modeExit fullscreen mode

Code References

Form validation

<!-- asp-validation-summary, asp-for, asp-validation-for --><formmethod="post"><divasp-validation-summary="ModelOnly"class="text-danger"></div><inputtype="hidden"asp-for="Products.ProductId"/><divclass="form-group"><labelasp-for="Products.ProductName"class="control-label"></label><inputasp-for="Products.ProductName"class="form-control"/><spanasp-validation-for="Products.ProductName"class="text-danger"></span></div></form>
Enter fullscreen modeExit fullscreen mode

Import external JavaScript file

@*@section,Scripts,Html.RenderPartialAsync*@@sectionScripts{@{awaitHtml.RenderPartialAsync("_ValidationScriptsPartial");}}
Enter fullscreen modeExit fullscreen mode

Custom tag helper

publicclassEmailTagHelper:TagHelper{privateconststringEmailDomain="contoso.com";// Can be passed via <email mail-to="..." />.// Pascal case gets translated into lower-kebab-case.publicstringMailTo{get;set;}publicoverridevoidProcess(TagHelperContextcontext,TagHelperOutputoutput){output.TagName="a";// Replaces <email> with <a> tagvaraddress=MailTo+"@"+EmailDomain;output.Attributes.SetAttribute("href","mailto:"+address);output.Content.SetContent(address);}}
Enter fullscreen modeExit fullscreen mode

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Coding and technology enthusiast.
  • Location
    Hong Kong
  • Joined

More fromSteve Mak

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp