Showing posts with label MVC6. Show all posts
Showing posts with label MVC6. Show all posts

Friday, April 15, 2016

Basic use of TagHelpers in ASP.NET Core


TagHelpers are a great new feature in your MVC Views to assist you in generating HTML code. The advantage is that the syntax is aligned with "normal" HTML elements and attributes. It is a new extension that is handled server-side by Razor, but the code feels more like HTML.

What is the problem with Html.Helpers?

@Html.ActionLink("Name", "Index", new { sortOrder = ViewBag.NameSortParm, currentFilter = ViewBag.CurrentFilter })


This would generate HTML anchor code (<a href>) with specific parameters. One of the issues is the way new objects are created inside the method.

Previously you probably used HtmlHelpers to achieve different stuff in your views, but some stuff was hard to do and looked really bad (in my opinion). One of the things I had a hard time getting used to was applying style to the elements where I used Html Helpers. This is now much more intuitive using Tag Helpers. 


How to use TagHelpers 101


Step1: In your project.json file all you need is the TagHelpers Nuget package. 
"Microsoft.AspNet.Mvc.TagHelpers": "6.0.0-rc1-final",

Step2: Start replacing HtmlHelpers with TagHelpers in your View files.
In cshtml using Razor you would previously use the @Html.Actionlink and the syntax below.
@Html.ActionLink("This is a link", "About")

Now, using Tag Helpers. In cshtml and Razor you now achieve the same with this syntax.
<a asp-action="About" asp-controller="Home">This is a link</a>

They will produce the same HTML, but I find this syntax to be much more intuitive as it doesn't hide the anchor element/attributes. You will see later that it becomes easier to apply styles etc. on the elements.  

Example Code (asp-controller, asp-action, asp-for, asp-validation-xx)

I picked this small piece of code from my Event handling system where categories are registered using a file similar to the one below. (Views\Category\Create.cshtml). 

@model MFAEvent.Models.Category
@{
    ViewBag.Title = "Create a new category";
}
<h2>@ViewBag.Title</h2>
<form asp-controller="Category" asp-action="Create" method="post" role="form">
    <div asp-validation-summary="ValidationSummary.ModelOnly" class="text-danger"></div>
    <label asp-for="Name"></label>
    <input asp-for="Name" />
    <span asp-validation-for="Name" class="text-danger"></span>             
    <label asp-for="Description"></label>
    <input asp-for="Description" />
    <span asp-validation-for="Description"></span>                                     
    <button type="submit">Create</button>             
</form>     

(bootstrap code is removed from the sample to make it more readable).

Just let me know if you need to entire example and I will post it or mail it to you. :-) 

Thursday, April 14, 2016

How to Create an ASP.NET Core MVC6 Web Application using EF, Identity, Bootstrap and more - Planning and Overview


Introduction

In this series of post I will create a small event handling system entirely in ASP.NET Core. The idea is that it will be built using ASP.NET Core and MVC 6. The main idea is to explore many of the new features in ASP.NET Core. Hopefully there will be some learning in it for you as well. 

The example is available here -> http://mfaevents.azurewebsites.net and will be updated as new features are added.


The initial menu - using AdminLTE/Bootstrap for layout.


The plan is to make an MVC Web Application for handling events, guests and enrollments. In addition I would like to create a simple crossplattform app that can be used to simply check people into the event on arrival. If time permits and you still find it interesting I might create an additional app so that the guest can handle their own profile, enrollments and even an event schedule of some sorts. 

In this post I will keep the overview of the solution as we go along.


Models:

Events
Guests
Enrollments
(Venues) -later
The first step is to create the models, controllers and simple views for basic CRUD methods (create, update, delete).I will start by creating a New Project using the new ASP.NET Web Application (including local authentication). After VS creates the initial structure I add som additional folders. These are the different places I like to place my code going forward. The most important folders are; Controllers, Models, Views and ViewModels.



References contain all the NuGet packages. 

Dependencies contain all the client-side stuff handled with Bower/Npm.