Introduction to ASP.NET Core part 16: tag helpers cont’d with forms

Introduction

In the previous post we started discussing a new feature in .NET Core MVC, namely tag helpers. There are numerous tag libraries available out there but for .NET it’s something new. The built-in tag library allow us to create dynamic URL using more HTML elements and attributes and less C#. The current version of the tag library offers many HTML-based alternatives of the Html helpers. The tag library elements are transformed into real HTML on the server side so the end users never get to see them and there’s no need for any magic JavaScript library to convert attributes such as “asp-action” into “href” on the client side. Occasionally though we have to revert back to using the Html helpers since not all functionality is available through the tag library yet. We can however expect it to grow more and more in the future as .NET Core matures. Using either the Html helpers or the tag library is largely a matter of taste. If you’re fine with a lot of C# in the Razor views then go with the Html helpers.

In this post we’ll continue to explore the built-in tag library and build a form using the asp tags.

Read more of this post

Selecting a subset of elements in LINQ C# with the TakeWhile operator

The TakeWhile extension method in LINQ is similar to Take. With Take you can specify the number of elements to select from a sequence. In the case of TakeWhile we can specify a condition – a boolean function – instead. The operator will take elements from the sequence while the condition is true and then stop.

Data collection:

string[] bands = { "ACDC", "Queen", "Aerosmith", "Iron Maiden", "Megadeth", "Metallica", "Cream", "Oasis", "Abba", "Blur", "Chic", "Eurythmics", "Genesis", "INXS", "Midnight Oil", "Kent", "Madness", "Manic Street Preachers"
, "Noir Desir", "The Offspring", "Pink Floyd", "Rammstein", "Red Hot Chili Peppers", "Tears for Fears"
, "Deep Purple", "KISS"};

We’ll keep selecting the items until we find one that starts with an ‘E’:

IEnumerable<string> res = bands.TakeWhile(b => b[0] != 'E');
foreach (string s in res)
{
	Console.WriteLine(s);
}

This will print all bands from “ACDC” to and including “Chic”. The last item to be selected will be “Chic”, as the one after that, i.e. Eurythmics starts with an ‘E’.

There’s an overload of TakeWhile where you can pass in the index variable of the loop. The following query will keep selecting the bands until we find one that starts with an ‘E’ OR the loop index exceeds 8:

IEnumerable<string> res2 = bands.TakeWhile((b, i) => b[0] != 'E' && i < 8);
foreach (string s in res2)
{
	Console.WriteLine(s);
}

This time Oasis is the last item to be selected as that is the 8th item, so the “i less than 8” condition was reached first.

View the list of posts on LINQ here.

Introduction to ASP.NET Core part 15: starting with tag helpers

Introduction

In the previous post we discussed a special type of view file in .NET Core MVC called _ViewStart.cshtml. This file can include a C# Razor code block which is executed before the concrete view files are rendered. The most common usage of the view start file is to declare the layout used for that view. That discussion brought us to layout files and how they can help us factor out common HTML markup from the views. Common HTML markup includes menu items, navigation bars, footers and the like. The content of the concrete view file is injected into the layout file using a method called RenderBody. The RenderSection function adds a named section in the layout that the views can implement with a names section directive.

In this post we’ll start covering a new feature in .NET Core MVC called tag helpers. It’s quite a sizable new topic in MVC .NET so we’ll dedicate multiple posts to these helpers since not even experienced ASP.NET MVC developers have been exposed to them.

Read more of this post

Creating lists in F#

Lists in F# are type safe and immutable. Type safety means that all elements in the list must be of the same type. Immutability implies that once the list is created we cannot add new elements to it. Instead, a new list will be created with the elements of the old list copied to the new list along with the new value(s) we want to add to it.

Here’s how to declare an empty list:

let myFirstList = [];

The ‘::’ operator is used to add a new item to the head of the list:

Read more of this post

Creating substrings in F#

Reading a chunk of a string into another string is based on the position of the characters in the string. The characters in the string form an array of chars and we can read parts of it by referring to the required array positions of the characters. This is no different from popular OOP languages like C# and Java. The only difference is the syntax.

Let’s start with the following string:

let longString = "The message couples the distressed heat before a purchase."

The random sentence generator site has a lot of similar meaningless sentences for testing.

The most simplistic application of substrings is to read one character:¨

Read more of this post

Introduction to ASP.NET Core part 14: the view start and the layout files

Introduction

In the previous post we looked at a special kind of view file called _ViewImports.cshtml. It is special in a way that MVC knows about this file and will pick it up during execution. We don’t need to register the file anywhere, we just need to put it in a folder where the views are stored. The view imports file has a very specific role. We can put a number of C# using statements that will be executed in all view cshtml files where the view imports file is applied. We can have multiple _ViewImports file in the project. We can have one in each folder containing the views, like /Views/Books, /Views/Customers etc. and each view file in those folders will pick up the view imports file in that folder. We can also have an overall view imports file in the Views folder as well. That imports file will be applied to every single view within the Views folder and its subfolders.

In this post we’ll look at another special file called _ViewStart.cshtml. We’ll also talk a bit about the layout file since it is strongly related to the view start file.

Read more of this post

Build array index ranges of an integer in C# .NET

Suppose that we have a large array of data heavy objects that are impractical to handle in a single go. Instead we can read batches of the array until all elements have been processed. It can then be useful to build a range of indexes for the objects in the array that we want to extract.

E.g. if an array consists of 357 objects and we want to read at most 100 elements from it in a single batch then the size of the batches will be as follows:

100
100
100
57

…and we want to extract the elements by their indexes as follows:

0, 99
100, 199
200, 299
300, 356

…where the first number is the array start index and the second number is the array end index. Since we’re talking about array indexes the numbers are 0-based of course.

Read more of this post

ultimatemindsettoday

A great WordPress.com site

iReadable { }

.NET Tips & Tricks

Robin Sedlaczek's Blog

Developer on Microsoft Technologies

HarsH ReaLiTy

A Good Blog is Hard to Find

Ricos Blog zu Softwaredesign- und architektur

Ideen und Gedanken rund um Softwaredesign und -architektur, Domain-Driven Design, C# und Windows Azure

the software architecture

thoughts, ideas, diagrams,enterprise code, design pattern , solution designs

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

Anything around ASP.NET MVC,WEB API, WCF, Entity Framework & AngularJS

Cyber Matters

Bite-size insight on Cyber Security for the not too technical.

Guru N Guns's

OneSolution To dOTnET.

Johnny Zraiby

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

%d bloggers like this: