Posts

Showing posts from October, 2020

Create Progressive Web Applications with Blazor WebAssembly

Image
This article is going to explain how to create a Progressive Web Application (PWA) with Blazor WebAssembly with an example. What is PWA The PWA is a web application that acts like a native application using modern standards. You can install PWA on your computer. You can add a shortcut on the desktop and launch it in the start menu. You can easily convert the Blazor application to PWA. Let's see what are things that need to create an installable web application. A web manifest (The web manifest is a JSON file that tells the browser about your Progressive Web Application) [manifest.json] HTTPS A Service Worker with a functional fetch hander (So that your browser will recognize it is an installable app) Let’s take a step-by-step look at how to create a Progressive Web Application through Blazor WebAssembly Create a new project In the create a new project window, choose Blazor App Configure Your New Project Enter the project name and location in Configure your n

Model validation: Rerun validation (TryValidateModel) in ASP.NET Core MVC

Image
This article is going to explain how to validate the model manually. If you want to calculate the value of a model and assign it to the model, you can use TryValidateModel to validate if the model is valid or not. Let us see with an example of how TryValidateModel works. The following is SampleViewModel. The [Required] attribute is included for all properties. That is, all properties are mandatory, so it is a must to assign value to them. public class SampleViewModel { [Required] public int MovieID { get ; set ; } [Required] public string Movie { get ; set ; } [Required] public string DirectedBy { get ; set ; } [Required] public int ReleaseYear { get ; set ; } } Next is a controller Index() action method. Here the value is assigned to MovieID and ReleaseYear, but not to DirectedBy and ReleaseYear. public IActionResult Index() { SampleViewModel model = new SampleViewModel() { MovieID =1, ReleaseYear = 2019

ModelState (System.Web.Mvc) with example

Image
ModelState refers to a set of name and value pairs submitted to the server during a post. This allows you to save the value submitted to the server and check for errors associated with each value. Let's see how this works with an example. The following is a sample model code. It has three properties: MovieID, Movie and Release Year. SampleViewModel.cs public class SampleViewModel { public int MovieID { get ; set ; } public string Movie { get ; set ; } public int ReleaseYear { get ; set ; } } Below is an index view code. Here you can see how the model properties are bonded to the TagHelpers. Index.cshtml @ { ViewData[ "Title" ] = "ModelState Example" ; Layout = null ; } @model SampleViewModel <form action= "/Home/index" method= "post" > <div> <div> <label for = "MovieID" >Movie ID:</label> <input id= "MovieID"