Skip to content

ASPNET Core 5 MVC Bootstrap Responsive Web Programming Lecture 2 Views and Controllers

FurkanGozukara edited this page Nov 16, 2026 · 1 revision

ASP.NET Core 5 - MVC - Bootstrap - Responsive Web Programming - Lecture 2 : Views and Controllers

ASP.NET Core 5 - MVC - Bootstrap - Responsive Web Programming - Lecture 2 : Views and Controllers

image Hits Patreon BuyMeACoffee Furkan Gözükara Medium Codio Furkan Gözükara Medium

YouTube Channel Furkan Gözükara LinkedIn Udemy Twitter Follow Furkan Gözükara

#SoftwareDevelopment #Software #Engineering #CSharp #Coding #Course

Asp.NET Core V5 - MVC Pattern - Bootstrap V5 - Responsive Web #Programming with C# Full Course Playlist : https://www.youtube.com/playlist?list=PL_pbwdIyffsnAWtgk4ja3HN3xgMKF7BOE

GitHub repository of the course : https://github.com/FurkanGozukara/Responsive-Web-Design-With-ASP.NET-5-and-MVC-Pattern-and-BootStrap-5-2021

Discord channel link of the course : https://discord.gg/6Mrb8MwteQ

How to use Discord : https://youtu.be/AEwPtYiLvsQ

In Lecture 2:

  • In depth learning of Razor syntax

  • How to define variables in Views (cshtml)

  • How to write mixed HTML and C# code in views

  • How to use foreach, if, else, for, while, switch in Views with Razor syntax

  • How to compose custom classes in an ASP.NET MVC Core application and utilize them inside Views

  • How to define local methods and use them inside Views

  • How to define dynamic templated delegates and use them inside Views for printing easily HTML formatted data

  • Templated delegate vs methods inside Views

  • What are ViewData and ViewBag

  • What is the difference between ViewData and ViewBag

  • How to use ViewData and ViewBag to transfer data from Controller to the View

  • What are MVC Controllers

  • What are Actions in MVC Controllers

  • How does default routing matches controllers and actions

  • What are the action types such as HttpPost and HttpGet

  • How to call different actions inside MVC controllers

Please subscribe and make comments.

This course requires fundamental knowledge about programming and C#. So please watch our below playlists related to C#.

Our courses playlists are like below:

[1] Introduction to Programming Full Course with C# playlist : https://www.youtube.com/playlist?list=PL_pbwdIyffskoSXySh0MdiayPJsBZ7m2o

[2] Advanced #Programming with C# Full Course Playlist : https://www.youtube.com/playlist?list=PL_pbwdIyffslHaBdS3RUW26RKzSjkl8m4

[3] Object Oriented Programming Full Course with C# playlist : https://www.youtube.com/playlist?list=PL_pbwdIyffsnH3XJb66FDIHh1yHwWC26I

[4] Asp.NET Core V5 - MVC Pattern - Bootstrap V5 - Responsive Web #Programming with C# Full Course Playlist : https://www.youtube.com/playlist?list=PL_pbwdIyffsnAWtgk4ja3HN3xgMKF7BOE

[5] Artificial Intelligence (AI) and Machine Learning (ML) Full Course with C# Examples playlist: https://www.youtube.com/playlist?list=PL_pbwdIyffskVschrADCL6KEnL_nqDtgD

[6] Software Engineering Full Course playlist : https://www.youtube.com/playlist?list=PL_pbwdIyffslgxMVyXhnHiSn_EWTvx1G-

[7] Security of Information Systems Full Course playlist : https://www.youtube.com/playlist?list=PL_pbwdIyffslM_o92NwkaUzD7C6Fekx26

[8] (Turkish) Bilgisayar Becerileri Tam Ders playlist : https://www.youtube.com/playlist?list=PL_pbwdIyffsmyE2e909ea1MXLcMb8MenG

auto subtitle timing : NA

Video Transcription

  • 00:00:00 Hello students, welcome to lecture 2 of responsive  web design with asp.net 5 and MVC pattern  

  • 00:00:07 and bootstrap. Okay. So today we will continue  from the previous week. I will copy and paste  

  • 00:00:15 the code of the previous week. And so we  will have each one as separated folders.

  • 00:00:24 Okay, and I will make this lecture too.

  • 00:00:31 Okay, so I'm opening my solution.

  • 00:00:36 from the solution Explorer

  • 00:00:50 If you have any questions regarding the course,  please contact me from this course anytime.  

  • 00:00:58 This court is fully working  right now. You can find the  

  • 00:01:02 Discord link on our GitHub page here or under  the each video course video here, I think.

  • 00:01:15 Okay, here's and the GitHub  repository is here. Okay.  

  • 00:01:19 Okay, so it is open it is riding as lecture one,  but this is lecture to let's just rename it.

  • 00:01:35 Okay, and

  • 00:01:39 I wonder if we can rename this one as well.

  • 00:02:04 Look ahead. He named I will rename this to

  • 00:02:16 okay, so where we have left?

  • 00:02:23 I think

  • 00:02:28 where we have left is

  • 00:02:32 we have put that some if I remember.

  • 00:02:40 Okay, we have left here. So I am  opening that page to continue.

  • 00:02:50 Okay.

  • 00:02:52 We will continue with the  basic right razor scientists.  

  • 00:02:57 We already have idea about Droid Razr  scientists. I think we have seen this page. So  

  • 00:03:08 let's check it out. Okay. Yes.  Yes, we have made examples.  

  • 00:03:14 So this was the basic scientist of the razor  Pages. Yeah. You see we have we are able to access

  • 00:03:21 the server site variables with at  letters in front of the parameter name  

  • 00:03:33 or we can open and close  server-side coding like this.

  • 00:03:40 Even inside server-side coding  we can write HTML like this.  

  • 00:03:45 It is automatically  understanding the HTML tags and

  • 00:03:56 we can also write route put like this  because by default the outputs we are  

  • 00:04:04 putting on our page is encoded and encoded  means that they won't get parsad as HTML.

  • 00:04:14 Okay, and this is the commenting?

  • 00:04:20 Okay, so let's continue with  the next page of the tutorial.

  • 00:04:28 Okay variables.

  • 00:04:34 Just like in regular C sharp code. You can Define  variables in razor to store information for later.  

  • 00:04:39 Use if you are already inside a code scope  EG inside an if statement or another control  

  • 00:04:45 structure. You can just Define it straight  away. If you're inside of a markup scope,  

  • 00:04:51 you can use a razor code block as described  in a previous article to Define your variable  

  • 00:04:56 inside. Here's an example. Okay. So  this is the code scope as we can see

  • 00:05:03 Okay. So let's say here under this scope. We  are inside the code scope. Therefore. We can  

  • 00:05:13 Define variables like this like we are riding  a c-sharp quote in the code behind, okay.

  • 00:05:22 Sorry.

  • 00:05:25 Okay, and then if they are not  inside quotes scope. What can we do?

  • 00:05:35 That's it.

  • 00:05:42 Okay, we have to Define them inside called  scope or they have to be defined in our  

  • 00:05:48 model. I think there is no other way. So this  is the model reference that we are getting from  

  • 00:05:55 lecture one models movie. By  the way, the name space is not  

  • 00:06:01 rename it. Let's see if we can  rename namespace as well. So

  • 00:06:08 Okay, here's the namespace and let me  save it. I wonder Village rename them.

  • 00:06:15 Or they're not.

  • 00:06:18 Yeah this may cause a problem.

  • 00:06:26 okay, I was just

  • 00:06:28 Converting page. Okay. I can find all lecture  one and rename but I won't spend time with that.

  • 00:06:37 So let's continue. So this is how we Define and  access variables. So I will write some HTML HR  

  • 00:06:49 another HR. So if you want to watch this  HR, the HR element represents a paragraph  

  • 00:06:57 level telematic break eg a skin change in  a story or a transition to another topic  

  • 00:07:03 within a section of a reference  book and then we click

  • 00:07:06 Learn more it is opening the  main specification of HTML.

  • 00:07:16 and there is examples but

  • 00:07:22 these examples are not visible.

  • 00:07:30 Okay. Anyway, you will see that  so I will Define a new variable  

  • 00:07:35 inside the code block. So to open a  code block I put add character and  

  • 00:07:40 curly brackets open and close now. I am inside the  code block so I can write C sharp code as usual.  

  • 00:07:48 So I will Define a variable less this  time. Let's define a sitting list.

  • 00:07:54 okay, let's say

  • 00:08:00 list of names one moment. I have to pass.

  • 00:08:07 Okay, so list of names and let's put some names.

  • 00:08:13 Okay, such as

  • 00:08:16 Toto's

  • 00:08:19 University

  • 00:08:23 mercian and

  • 00:08:27 School, okay. Now I can access  this inside HTML tags or  

  • 00:08:35 inside another code snippet. So let's  access them inside some HTML tags such as  

  • 00:08:44 soap on. Okay, and how am I  going to access it is like this.

  • 00:08:51 First index then inside a div.

  • 00:08:57 Okay inside the new let  success as something like this.  

  • 00:09:04 So you see how inconvenient it is  to write. Let me check something.

  • 00:09:16 Oh not inconvenient. I think convenience I  have that. Yeah, you see how it is convenient  

  • 00:09:25 how it is easy to use the razor  scientists like this. I can also  

  • 00:09:32 write it as a code simple. Let me show you so here  we will do the opposite of writing them as a bow.

  • 00:09:43 Here I'm going to do a for each Loop and here I  will write simple HTML code is was not possible  

  • 00:09:50 inside web forms in the older version of  let's say the other version of asp.net based  

  • 00:09:59 programming. However, it is possible here. So what  I'm going to do is simply let's combine some more.

  • 00:10:09 Let's open an unordered ordered list.  Yeah, it is an ordered list. Okay,  

  • 00:10:17 you see how conveniently I can write and  here I will write each element like this.  

  • 00:10:24 Okay item and to item. I think  you should look like this.  

  • 00:10:31 Yeah. You see how convenient it is how easy  to write clean. It is really good. I can say

  • 00:10:40 When compared to all the others or let's  say and the other version of asp.net  

  • 00:10:50 programming so we can mix server  side code and HTML code as we want.

  • 00:10:59 with cleanest visibility and such

  • 00:11:11 Okay. Okay. So o of for each  Loop is not working as we wanted.

  • 00:11:22 Okay.

  • 00:11:26 Todos and Madison is printed as we wanted our  HR tags. Okay. However here is these are seen as

  • 00:11:43 These are seen as regular text  because they are honored HTML.  

  • 00:11:48 Therefore I have I will put an eight character  here and I will put at connector here.  

  • 00:11:56 Yeah now we should work. Okay. Now you see they  are colored differently and now I can see it is  

  • 00:12:05 displayed as a local variable.  Okay. So now let's run it again.

  • 00:12:26 Okay, it is working as expected. It is working  with great efficiency. I mean when you're  

  • 00:12:36 doing coding is the great way of coding which  efficiency cleans readable code manageable code.

  • 00:12:45 Okay. So this is what we have shown  this is also what we have shown.

  • 00:12:52 Okay, so when you have open it a course in  it, but if you are riding server-side code  

  • 00:13:01 or yeah service, thanks, but we may say or  variable defined code inside HTML tags. Then  

  • 00:13:08 you have to let the compiler know that which  one of them is text or which one of them is.

  • 00:13:20 Variable for example now that's inside  here. I can also something right like this  

  • 00:13:29 something. So these something will be text  and this will be variable. Okay, you see that?

  • 00:13:48 Okay, let's go to next.

  • 00:13:52 arts and

  • 00:13:55 yeah, you see something is printed  as text. Okay, so if statements

  • 00:14:04 when defining the mark-up for your views, it's  extremely useful to define a conditional statement  

  • 00:14:09 which decides whether or not a portion  of the view is interpreted and rendered  

  • 00:14:14 the most common conditional statement  is the if statement and you can use one  

  • 00:14:18 of these in your razor code pretty much  like you would in regular C sharp code  

  • 00:14:22 just prefix the keyword with the razor  operator at and you're good to go.

  • 00:14:29 The if statement here's an example of a simple  if statement with razor. Remember that this can  

  • 00:14:34 be included directly in your views alongside with  regular HTML. Okay. So let's make example of that.  

  • 00:14:43 By the way, I will remove this alert message to  not see it every time when we run the application.

  • 00:14:56 okay, where was the alert alert is here so

  • 00:15:02 I just need to comment it out. Okay?

  • 00:15:08 Okay, here's if they time now we're equal to 2042.  

  • 00:15:17 This pain will be printed. This is regular  seizure coat and let's make it also as 2021.

  • 00:15:29 So we can see which one of them is  working and mission of them not.

  • 00:15:38 Okay.

  • 00:15:52 As you can see, it's just C sharp mixed with  HTML. You should be aware of two things though.  

  • 00:15:59 First of all in regular C sharp. I could have  skipped the curly braces because there's only  

  • 00:16:04 one line of code markup as the condition. This  is not allowed in razor though. No matter how  

  • 00:16:10 many lines follows your control structures. They  have to be surrounded by a set of curly braces.  

  • 00:16:16 Second of all notice how I switch  directly from C sharp into markup.

  • 00:16:21 This is possible because the parser can easily  understand the difference between HTML tags and  

  • 00:16:26 c-sharp code on the other hand. If I had just  written a line of text without any HTML tags.  

  • 00:16:33 The parser would have been confused for  cases like that. You can use the less than  

  • 00:16:38 text greater than tags as described in the  previous article. Okay, so there are two key  

  • 00:16:43 points. Here's before we delve into that  you see our code is working it is here.

  • 00:16:50 What are the two key points  here it is that the parser can  

  • 00:16:58 easily understand difference between HTML tags  and decision support. This is very important  

  • 00:17:03 because of that we are able to write like this  and this and if you don't put any HTML tags,  

  • 00:17:13 then the person get confused. So  what does this mean this means

  • 00:17:22 Okay, so I have put something like this. It is  not inside an HTML tags under the code snippet  

  • 00:17:30 code block or let's say so you see now the  compiler is telling me that something is wrong  

  • 00:17:38 or the intelligence we can say because it  cannot find variables names like named like this  

  • 00:17:45 and they are not inside any HTML tag. Therefore.  It cannot understand it the difference. This is

  • 00:17:52 Us that takes I just want to write a text.

  • 00:17:56 To write just a taste. I put a text.

  • 00:18:04 Talk like this and now  whatever inside text will be.

  • 00:18:14 Evaluated as simple text not as a as  a variable or object or something.

  • 00:18:23 And he will see how it is printed on the skin.

  • 00:18:32 Okay, you see it is here. This means that it  is on the same line because we didn't use any  

  • 00:18:41 need line having HTML tag. So there is support and  by default one is display:inline. Okay. I think we  

  • 00:18:52 can see it here. You see display:inline spawns  or have display:inline features and therefore

  • 00:19:01 Is no new line after spawn and therefore it  is coming at just after this point itself.  

  • 00:19:08 I could have changed this  Behavior something like this.

  • 00:19:12 this play

  • 00:19:15 I Can Say

  • 00:19:20 Inline block not remember, okay.  I need to say as a block here.  

  • 00:19:25 So after block it will move to the next line.

  • 00:19:33 So you see I can I am able to Define display style  as well with this normally sitting character.  

  • 00:19:46 This was a really painful to write in regular verb  forms. You see now it is starting at a new line  

  • 00:19:56 because when you set this fly block display  block, it takes an entire line like this.

  • 00:20:04 So this is display block and this is  also display block as you can see.

  • 00:20:11 Or actually this is display list item. So this  is a list item is like this. This is Dave.  

  • 00:20:18 Therefore it is display block and this is spun.  Therefore it is display:inline, but it is not

  • 00:20:30 Okay here I think I we can see  the computer or cable lock.

  • 00:20:36 This is this playlist titled. Okay.  

  • 00:20:39 Okay. So let's see the if as statement you see how  it is convenient easy to write if else as well.

  • 00:20:51 Something like this. It is so simple so clean  writing. This code inside old web forms for us  

  • 00:21:01 extremely. Painful. We had to put a lot of  server-side parser opening and closing like this.

  • 00:21:12 Okay in the previous lecture  I have shown you that.

  • 00:21:18 So it is simple clean and convenient.  Okay, let's continue. Okay looks

  • 00:21:28 looping is an extremely useful programming  technique, which you can also benefit a lot from  

  • 00:21:33 in your razor code looping allows you to repeat  an action and or output for a specific amount  

  • 00:21:39 of iterations for instance to Output items in a  list as we'll see in the examples of this article.  

  • 00:21:45 There are several types of Loops in C sharp  and they are all available in your razor code.  

  • 00:21:51 Let's go through all of them using  the same very simple data source  

  • 00:21:55 a list of names which will  turn into an unordered age.

  • 00:21:58 Tml list. Okay here we are defining by the way  before going. That's let's put some break HR.

  • 00:22:14 Somatic breath. Okay. So you see my code is  currently not formatted to format it. I click Ctrl  

  • 00:22:22 K and D at the same time.

  • 00:22:33 It is something.

  • 00:22:38 All right.

  • 00:23:09 Uncle J and control at all. Okay, I got  confused control key was pressed you see here.

  • 00:23:18 And now it is telling me that waiting for a second  chair of court and then I will hit control deaf.

  • 00:23:28 mind control

  • 00:23:32 Oh control key and control. This is  the correct one control D + Ctrl e

  • 00:23:47 Interesting why it is not working.

  • 00:23:55 It is not the correct combination anymore.

  • 00:23:58 Control Katie. Yeah, this is the correct one.

  • 00:24:05 Although it has put so many empty lines  for some reason. So I will just Ctrl Z to

  • 00:24:17 Just fix it.

  • 00:24:26 Okay Auto format is not  working as expected right now.

  • 00:24:33 I don't know why but I would expect it  to promote this something like this.  

  • 00:24:39 Okay. Anyway, the shortcut is  Ctrl K. And plus the this one.

  • 00:24:49 Okay, so let's return to our topic.  So we have defined it at least  

  • 00:24:54 variable and initialize it  with these names obviously  

  • 00:25:01 and then we can use love I had already made its  example here now they have used inordinate list,  

  • 00:25:12 but the poor look yes, let's write a for loop  as well. You can use any Loops. It is same as

  • 00:25:19 C sharp like this or do while loop?

  • 00:25:27 Like this so you see there is a counters.

  • 00:25:31 Variable defined as here's actually  I put Define the commentary  

  • 00:25:35 here as well. And since they are in the same  level, what is the level the level is there?

  • 00:25:47 I mean the same level so this is in  the level 0 this code scope is also in  

  • 00:25:54 level 0 and this is also in level  0 since they are in the same level  

  • 00:25:58 of hierarchy you can access  this variable at here as well.

  • 00:26:05 So when we run the application,  we will see they are all working.

  • 00:26:12 Now we can do do while as well. So  let's change a bit to understand.

  • 00:26:21 What I'm going to do is I will write some.

  • 00:26:27 Text tears. Okay, since this is  not this area is not under any.

  • 00:26:35 Text I can I mean under any costly, but I can  write like this following link is for Loop.

  • 00:26:48 And he is following is so it  has automatically formatted  

  • 00:26:56 as you have seen was here and it put it  here automatically. This is wild wolf.

  • 00:27:06 and this is

  • 00:27:13 how long is the wild?

  • 00:27:20 Okay. So what is the difference between  while and do-while the difference is that  

  • 00:27:28 with two wires? Even if this condition  is not much it will prints one time.

  • 00:27:37 Okay, and which ball with the condition  check in here? Let's say you have the list  

  • 00:27:48 or five items. And if your contr is 5  this will not be executed. However here  

  • 00:27:59 it will be executed. Then the check will  be made here then it will terminate. Okay?

  • 00:28:07 Okay, so this plus plus will be also  executed after getting the index.  

  • 00:28:13 I believe what this means is so first it will get  the zero basis the first index of the name list,  

  • 00:28:27 then it will increase it. So this is executed  after this counter variable is used inside get

  • 00:28:35 Hot off the list. Okay, so when  without it you will understand it.

  • 00:28:42 These are all about writing C sharp code 2 plus  plus is always executed after the value of the  

  • 00:28:51 increased variable is read, okay.

  • 00:28:57 Okay, so you see they are all generating the same  

  • 00:28:59 output. This is followed. This is  while loop and this is to wire look.

  • 00:29:08 It's you can also see this Behavior like this.  Okay, I will put a breakpoint here and here and

  • 00:29:20 Yes, okay. So let's restart updation.

  • 00:29:28 I will try to explain you the  order of the code execution.

  • 00:29:38 Actually, this is something that I teach  in the first year, but no harm. Okay,  

  • 00:29:44 first the integers I is defined inside  for Loop. This will be only executed  

  • 00:29:50 the first time when the polo is starting then  it will be ignore it then it will check the  

  • 00:29:57 condition. The condition is checked.  The I is 0 therefore the condition  

  • 00:30:01 is met it is the model than the number  of elements in the names list. It is 5

  • 00:30:08 Okay, this part of the executed and when it is  going out of scope of the for Loop, this will  

  • 00:30:15 be executed and it will increase the I by 1 then  it will recheck the condition if it is melting  

  • 00:30:23 it will continue to talk if it is not it will  terminate. Let's continue to wire. Okay here  

  • 00:30:30 it will first check the condition. The counter is  0 again because we have defined the counter here.

  • 00:30:38 And then it will first read the first element of  the list because I am then it will increase it if  

  • 00:30:51 I step into or it didn't say show me. Okay here we  can't see but from the behavior, we can understand  

  • 00:30:59 that first value of the country's read here. Then  this plus plus is executed. Okay, it is not like

  • 00:31:07 The column to the Vault and  then get the element of the  

  • 00:31:11 index get element of the names list. It is first  read counter. It is zero in the first Loop get the  

  • 00:31:20 element in the names list. The first one  then increase counters it is saying here.

  • 00:31:28 Okay, let's continue and then  there is Bridge continue.

  • 00:31:36 So it is working same as C sharp  code or any programming languages.

  • 00:31:43 Okay, here we are doing a for Loop you are  printing the names list eat item. So it  

  • 00:31:51 is 0 1 2 3 if the counter is bigger than  2 or bigger than bigger or equal to 2  

  • 00:31:58 then we put another line and we break  this will Bridge the loop part Loop, okay.

  • 00:32:16 so first what we are going to see is we will see  

  • 00:32:21 the first three elements than this  line and then it will be and it

  • 00:32:31 Okay, let's continue.

  • 00:32:34 Okay, first three elements  and then it is and it's okay.

  • 00:32:40 And okay, we can move to  next part of the tutorial.

  • 00:32:53 Okay, this switch statement. It is no different.

  • 00:32:58 We previously used if statements in our razor code  to control code flow, but they are not the only  

  • 00:33:04 conditional statements available. When you write  your razor code and alternative exists in the form  

  • 00:33:10 of the switch statement. You may already know  it from C sharp or other programming languages,  

  • 00:33:15 but in this article, we will see how you can use  it in your razor code a switch statement works  

  • 00:33:21 by defining a condition to evaluate and then one  or several possible outcomes of this condition.

  • 00:33:27 So for instance if we have a user-specified  number in a variable and we want various outcomes,  

  • 00:33:32 depending on what it is. It could look  like this with a switch statement.  

  • 00:33:37 Okay. So let's make an example of switch  statement. So when I write something like this,  

  • 00:33:44 it won't work, of course. So what I need to  do is I need to make this switch statement  

  • 00:33:51 as a server-side code then I need  a variable name it as number.

  • 00:33:57 But I don't have it anywhere. So  what I'm going to do is I will even  

  • 00:34:03 make this even bigger something like this.

  • 00:34:13 Here you see it is it is automatically formatted.  I seen the razor but it is automatically  

  • 00:34:18 formatted. Okay, then I will make a loop of 100  and each time whenever the number or less a for  

  • 00:34:32 each case of number. I can write something.  So if number is 1 I will write number is one.

  • 00:34:42 Well, I want to show you something.

  • 00:34:51 Okay book this work now because it is still  treating this line as a server-side code. However,  

  • 00:34:58 it is not unless a c-sharp code so I have  to put it inside an HTML talk or text tag.  

  • 00:35:07 So let's make put it inside the div, which is HTML  tag, and it will automatically understand that.

  • 00:35:14 Okay, not you. See now if the case is it will  

  • 00:35:18 write as it like this and hear it for the right  like this. I can put any anything inside the

  • 00:35:28 Cases, so when I run the  application now, we will see.

  • 00:35:33 See, which is extremely useful if you have  

  • 00:35:36 many conditions to check so do not use if as  or if switch is the very you should prefer.

  • 00:35:48 in such cases

  • 00:35:52 a razor switch statement looks just like it does  in c-sharp but with an important difference,  

  • 00:35:57 you can include HTML directly inside your case  blocks allowing you to easily output markup and  

  • 00:36:03 text for each case all so unless you are already  inside a razor code block, you will need to  

  • 00:36:09 prefix the switch key word within a character  just like we have seen in previous examples.  

  • 00:36:14 Here's a complete switch example.  Okay, so as you can see current switch

  • 00:36:23 Codebook is working as expected and I  will copy and paste this one as well.

  • 00:36:30 So let's ride. It is automatically formatted. So  it will take the number of the day or day of the  

  • 00:36:39 week from the daytime now, it will be my local  time basis. And if it is Monday, let's write as  

  • 00:36:47 it is Monday. So today is Sunday and if path  default means that if not, any of these cases  

  • 00:36:56 is satisfied the default will happen.  Currently we are we don't have

  • 00:37:00 When is the Darfur we will get into default?

  • 00:37:08 These are all regular C sharp programming paths  

  • 00:37:12 no harm to be seeing time learning come  again. So meanwhile, let's move to next.

  • 00:37:20 And you seen others nothing special  about this day as you can see.

  • 00:37:25 All right, so the local functions

  • 00:37:32 In a previous article we talked about how razor  allows you to define a block of code inside your  

  • 00:37:37 views like this. Okay. We are still inside  a view. Also you see in the next CSS HTML,  

  • 00:37:46 by the way, when I do auto format, which control  KD it put some empty lines. I think it is where to

  • 00:37:56 My comments, so I'm just letting them.

  • 00:38:00 Okay.

  • 00:38:03 You can use these code blocks  for pretty much anything that C  

  • 00:38:07 sharp can do you can even Define local  functions. This is really cool because a  

  • 00:38:12 local razor function allows you to include  markup which makes it possible to create  

  • 00:38:17 small template functions. This is useful  in a number of situations for instance as  

  • 00:38:22 we can see in this next example where we need to  generate the same piece of markup multiple times  

  • 00:38:28 defining a local function is just like  defining a class method except a local

  • 00:38:33 Doesn't have an access modifier  public-private Etc. Here's how it could look.

  • 00:38:41 Okay, so it has least web users.

  • 00:38:50 Okay, so I will Define a  local function and call it.

  • 00:39:01 It says model next.

  • 00:39:11 Okay, they didn't include the code behind  of hello. Mr. Ward models web users.  

  • 00:39:21 Therefore. I will use my I will generate my own.

  • 00:39:31 model so

  • 00:39:35 you will understand it as well and seconds.

  • 00:39:43 Okay, so when I copy and paste this  code, it won't work because we don't have  

  • 00:39:50 a class as hello. Missy word modest web user.  Therefore first. I will define a class plus  

  • 00:39:58 web users and inside this class.

  • 00:40:03 We will have public property as first name.

  • 00:40:10 Okay.

  • 00:40:13 I can also Define Fields such as

  • 00:40:19 last name

  • 00:40:23 the difference of fields and

  • 00:40:28 properties I explain them in my object-oriented  programming and advanced programming lecture  

  • 00:40:35 lectures courses so you can watch them on our  

  • 00:40:39 YouTube channel. So here it will  take parameter. Dark web user.

  • 00:40:45 And then it has birthday.  This is probably daytime. So

  • 00:40:53 Birds Day by the way, I will assign  this by default date time now.

  • 00:41:00 I can also Define it at the moment of  initializing an instance of that users.  

  • 00:41:08 Okay, here it is working. It has a user class  user info. So let's put some styling on it such as

  • 00:41:17 Okay, let's add a font color.

  • 00:41:21 from here

  • 00:41:25 as we grow, okay, here's

  • 00:41:29 and

  • 00:41:32 it has a list of web users. So what I need to  do is I need to compose an object list of web  

  • 00:41:46 users. So how am I going to do is simply this  web users and I will name it as my accusers.

  • 00:41:57 A sniff of uses. Okay, and inside  this list I will compose each one  

  • 00:42:05 of the web users instances something like this.  

  • 00:42:09 Okay. So the birthday will be equal to such  as NIV date time and let's generate some.

  • 00:42:19 Birthdays mm.

  • 00:42:24 Okay, 12 fun.

  • 00:42:27 and then first name as

  • 00:42:33 let's put

  • 00:42:37 some classical name last name.

  • 00:42:42 as okay

  • 00:42:46 and let's continue another web users  

  • 00:42:49 as this time. I will leave out the birthday.  It will be daytime now and first name equal to

  • 00:43:00 and the last name is equal to

  • 00:43:05 let's say something like this and another one as

  • 00:43:13 let's define a birthday for this one.

  • 00:43:18 and let's say

  • 00:43:22 H past or less 855. Okay. First name as

  • 00:43:34 let's see.

  • 00:43:38 and last name as

  • 00:43:42 let's say

  • 00:43:49 I don't know if there's such last night. Probably  there are okay. So and we have to put this.

  • 00:44:08 okay, do we put expected it is  expected because I think we have to

  • 00:44:23 Yeah, this will be like this.

  • 00:44:34 No, no not like my kisses.

  • 00:44:39 Okay, what is wrong with my okay?  

  • 00:44:43 Okay now no errors. Yes. We have defined  our web user list it will now for each.

  • 00:44:55 Did my refuse a list with order by?

  • 00:45:00 However, as you can see this is dropping  an error why because our coats scope.

  • 00:45:12 Is not and yet why then it is  giving an error. Let's see.

  • 00:45:18 Evil token for each in class because  stock interface member declaration.

  • 00:45:27 trusting God

  • 00:45:31 Okay, we are inside the court and it is ending  here we have for each it calls random user info.

  • 00:45:42 We have HTML here.

  • 00:45:48 So what is not working?

  • 00:46:03 Amo talking for each in class records  fact or interface member declaration.

  • 00:46:16 Becky just because of

  • 00:46:21 this one it has to do differently.

  • 00:46:25 All right. What is this?

  • 00:46:30 And it will be type of birth users. Okay in

  • 00:46:35 my web users

  • 00:46:45 Okay in my web users, which is here.

  • 00:46:50 It says order by.

  • 00:46:54 Okay.

  • 00:47:22 What is this?

  • 00:47:33 Okay, we have Define class here because we have  defined it variable here and we have defined it  

  • 00:47:50 Metals here now like we can't Define.

  • 00:48:00 Okay, this is working but  why for each not working?

  • 00:48:16 That's checked it. I don't know it says that.

  • 00:48:29 CS 1519

  • 00:48:36 Okay, let's see.

  • 00:48:38 So we have a compiler error  and what is maybe happening?

  • 00:49:11 Okay, because this doesn't exist in you.

  • 00:49:16 And no.

  • 00:49:18 It says it my web users does not  exist in the current context.

  • 00:49:42 What is it should be exist? Why  maybe we have to Define it as

  • 00:49:48 public

  • 00:50:18 it doesn't exist.

  • 00:50:21 Babe, I need to make this public.

  • 00:50:47 Let's remove this and see what happens.

  • 00:50:54 Let's movies as well.

  • 00:51:01 We are still inside. What work.

  • 00:51:06 So what is causing that it doesn't exist?

  • 00:51:43 So the total for each in class record  struck or interface number nectar Nation.

  • 00:52:02 This is the first time I am getting such an oaf.

  • 00:52:12 So it is related to probably our  compiler. What I'm going to do is simply  

  • 00:52:21 I will end the code snippet here. Then I will make  them separate code and I will remove this now.

  • 00:52:34 Let's see what happens.

  • 00:52:42 Okay, I got you. Okay.

  • 00:53:07 What is the problem?

  • 00:53:15 I have so much fun of us.

  • 00:53:19 Do we have an uncle those words?

  • 00:53:23 Yeah, yeah. Do you know why because  you see we have close with it?

  • 00:53:29 They have closed it them.

  • 00:53:38 And let's remove this unless if it is working.

  • 00:53:44 Or maybe we have a problem somewhere else.

  • 00:53:56 So, let's see the others.

  • 00:54:05 Okay, we have model. Yeah, we  have a practice is ever so much.

  • 00:54:12 But worse.

  • 00:54:14 Says it is expected by.

  • 00:54:29 on this automatic generation

  • 00:54:35 Okay. Sure.

  • 00:54:41 Can we have ever somewhere but we need to find it.  

  • 00:54:46 It says that the collaborated is  expected here. Can we have seven each?

  • 00:54:57 We have switch here.

  • 00:55:01 test kids bad kids bad kids make

  • 00:55:05 later to this court

  • 00:55:09 if feeling movies, let's see if it's working.

  • 00:55:16 So, you know this as well.

  • 00:55:26 That's the movies true.

  • 00:55:34 You know compiled. So what we're going to  do is let's leave this class definition  

  • 00:55:44 definition. Okay, so it was  about our class declaration.

  • 00:55:53 our class declaration is

  • 00:55:56 causing a rash

  • 00:56:07 So it was our problem.

  • 00:56:33 Okay, so it appears that we are we cannot define a  class inside a razor page. So it was our problem.  

  • 00:56:43 Therefore we have to Define this class inside our  model also it is it is making sense. You should  

  • 00:56:50 not Define your classes in razors. You should  Define them in your model. That is why we have.

  • 00:56:59 I'm EC model control the live.

  • 00:57:05 Okay. I have moved my class  here. Then this will be in my

  • 00:57:17 model dot actually

  • 00:57:23 Yes.

  • 00:57:28 All I'm not going to access it.

  • 00:57:42 I think I will access it in small doses.

  • 00:58:05 Oh, I cannot access it directly like this. Why  because we are returning movie class and this is  

  • 00:58:12 another class therefore to I if I want to  access it, I have to Define it like this.

  • 00:58:21 And then I can access it via it.

  • 00:58:35 Okay, how can I access it?

  • 00:58:42 More than one of me.

  • 00:58:52 Looking more than release date title.

  • 00:59:15 I think I have to add the reference as

  • 00:59:50 the model and The View

  • 01:00:26 maybe something like this.

  • 01:00:31 Can use ink?

  • 01:00:34 Lecture Bond model starts. Yeah.

  • 01:00:40 here I will make it as

  • 01:00:44 like this

  • 01:00:46 but users so it will be available.

  • 01:00:52 like Siobhan

  • 01:00:55 models

  • 01:01:13 I'm making something like this.

  • 01:01:20 Yeah, let's check it out now.

  • 01:01:26 And here's okay now it is visible.  Okay, so we have solved the problem  

  • 01:01:31 now, it shows that you see  lecture 1 models web user class.

  • 01:01:37 And I have defined a local  function here and now it will work.

  • 01:01:46 Okay. So the first case will be  ordered by this is linked here  

  • 01:01:53 x means each element inside the class x dot first  name and this is by default a scanning order.

  • 01:02:03 For sorted by first name and  

  • 01:02:07 then the second pain. I think I can also sort  them by their birth date as well last name.

  • 01:02:17 Okay, we have one extra.

  • 01:02:21 connecting here

  • 01:02:23 yeah, okay and

  • 01:02:28 done use the sorted by vertex.

  • 01:02:36 Okay.

  • 01:02:41 Okay sorts a scamming by the way.

  • 01:02:49 Okay. Now as you can see, I am able to call my  

  • 01:02:56 local method. However, it turns out that  we are not able to define local classes.

  • 01:03:11 I have but I can check it out.

  • 01:03:15 This think was inside class. No, not that one.

  • 01:03:23 Actually, it is also against idea of  MVC so you should not use it. Okay,  

  • 01:03:27 so use the sorted by a scanning by first  name, then we GI share Google uhm a  

  • 01:03:34 Torquay and sorted by last name where hmm?  Hmm. See I see you see there are scanning  

  • 01:03:40 order which lie under are sorted  by date. Yeah. It appears that my  

  • 01:03:48 example was perfect case for sorting.

  • 01:03:54 Okay.

  • 01:03:59 Let's see.

  • 01:04:02 All right.

  • 01:04:10 Okay, we have seen the local  functions as well methods.

  • 01:04:17 Okay templated delay rates.

  • 01:04:26 template it delegates

  • 01:04:32 The templated delegates functionality in razor  allows you to define a piece of markup and then  

  • 01:04:37 have it used to represent a specific object  on the page. This is a great tool when you  

  • 01:04:43 need to represent EG one of your classes  in multiple places inside of your view.  

  • 01:04:48 It can also be a great way to separate a  large chunk of markup from the logic of  

  • 01:04:52 your view which can make it easier  to read and maintain large views  

  • 01:04:57 when defining a templated delegate  you will be using a funk delegate it.

  • 01:05:01 Look like this.

  • 01:05:04 Okay, so there is an example function  Dynamic object movie template.

  • 01:05:12 By the way, this a delegate then it  uses div for formatting items title.  

  • 01:05:20 And then this is text because  it is inside the div and then  

  • 01:05:25 item release date too short a time to  drink and is also text inside the okay.

  • 01:05:32 Let's continue reading.

  • 01:05:35 In this first half before the equal sign, I create  the movie template delegate in the second part. I  

  • 01:05:42 specify the mark-up template to be used notice  that I use a variable called item. It's passed  

  • 01:05:48 into the delegate and the type is dynamic meaning  that I can access members on it EG title which are  

  • 01:05:53 not checked at compile time instead. They are  validated at runtime where they are expected  

  • 01:05:59 to exist on the object passed to the delegate.  If not an exception will be thrown I intend on

  • 01:06:05 Passing an object's of the type movie a simple  class. We used previously in this tutorial.  

  • 01:06:11 Okay. So this is not controlled at  compile time. Therefore you should  

  • 01:06:18 you have to be careful if you are using runtime  checking's okay. So I think we Define it is side.

  • 01:06:29 Code block. Yeah, we have to be find it in  side control. So this is our already model.

  • 01:06:40 And now we can use it inside delegates.  Okay. So how different it is from  

  • 01:06:50 a local methods? Let's see it.  

  • 01:06:54 Okay. We will Target our title song by  the UCB of data title is signs are title.

  • 01:07:04 You can also use it or we are  able to write it like this.  

  • 01:07:08 It will say about this data later, I believe.

  • 01:07:16 so

  • 01:07:20 By the way, the data is the data that's  coming from the model. Probably. Yeah.  

  • 01:07:26 Yeah, this is about

  • 01:07:29 they data that is coming from

  • 01:07:33 model

  • 01:07:47 so I will try to print it on scream like this.

  • 01:07:58 One moment. Here's I will open  and thief or let's say h 1.

  • 01:08:06 Okay view the data. I don't equal to if Theta.

  • 01:08:16 We also have you back and  let's see what so we have data.

  • 01:08:26 Get so six sets the diction of  the for real data and real Beach.

  • 01:08:33 So what is the difference  between give back and give data?

  • 01:08:42 Okay, it is already asked previously.

  • 01:08:50 So trust our data from controller to be  big data is nothing but a dictionary of  

  • 01:08:54 objects and it's accessible by string ksk  real data is a property controllers from

  • 01:09:06 Okay, so we have data versus me, bitch.

  • 01:09:12 View data and view bag are used for the same  purpose to transfer data from controller to view  

  • 01:09:18 view data is nothing but a dictionary of  objects and it is accessible by string as key  

  • 01:09:23 view data is a property of controller that exposes  an instance of the view data dictionary Class  

  • 01:09:30 View bag is very similar to view data view  bag is a dynamic property Dynamic keyword,  

  • 01:09:35 which is introduced in.net framework 4.0.

  • 01:09:39 Viewbag is able to set and get value  dynamically and able to add any number  

  • 01:09:44 of additional Fields without  converting it to strongly typed  

  • 01:09:48 view bag is just a wrapper around the view  data. Okay, so there are some examples.

  • 01:09:55 So this is controller code action results.

  • 01:10:01 Okay, so let's add example of our  

  • 01:10:08 view data and Vivek. So here we edit it to our  controllers such as lists through student and  

  • 01:10:19 PSI knit 2 hours. We've data s with the keyboard  student and each Edge list of see drink.

  • 01:10:30 reference to the student

  • 01:10:34 and then there is a view back  example. I will do the same.

  • 01:10:41 Okay, you see with the vbac we  are assigning it as dynamic.

  • 01:10:48 So it will be solved at result at the wrong  time. And this is s this is similar to session,  

  • 01:10:57 you know with keyboards. However,  we are adding a object here.

  • 01:11:04 Dan we can use them in our view such as okay.  

  • 01:11:12 You see this is all the code not  the case anymore with razors.

  • 01:11:19 Okay. So here's the key point is we Define  its type with s okay in the Viv data and we  

  • 01:11:31 don't need these anymore because we can we are  using razor and therefore it is much better.

  • 01:11:44 In scientists and convenience.  Okay and for Viv batch.

  • 01:11:54 Let's see.

  • 01:11:58 Should students

  • 01:12:02 I think it's the same.

  • 01:12:06 Oh, we don't even need to say okay. One moment.

  • 01:12:17 Okay, it doesn't come but we can  directly say it don't like this.

  • 01:12:25 Okay, so

  • 01:12:29 this will be

  • 01:12:32 let's put tea.

  • 01:12:42 use data and

  • 01:12:47 you back.

  • 01:12:58 It isn't on the court.

  • 01:13:02 So you see these are this is fully Dynamic  and this is working as a dictionary.

  • 01:13:12 In both cases, they are not checked at  around at the compile time and they will be  

  • 01:13:22 controlled at runtime. So if  an error happens it will be on.

  • 01:13:26 Catch it. Uncle's okay students in live data  and students in we wake they are robot working.

  • 01:13:37 Are there any other difference?

  • 01:13:43 It is also temp data. It is another  thing. Let's read also about that.

  • 01:13:50 temp data is a dictionary which is  derived from temp data dictionary class  

  • 01:13:56 temp data is stored data just like live  session for short time temp data keeps data  

  • 01:14:02 for the time of HTTP request which means that  it holds data between two consecutive requests  

  • 01:14:08 temp data helps us to transfer data  between controllers or between actions  

  • 01:14:14 temp data internally use session variables note  that temp data is only work during the current and

  • 01:14:20 Subsequent request it is generally used to  store one time messages with the help of  

  • 01:14:26 the temp data keep method. We can keep value in  temp data object after request completion. Hmm,  

  • 01:14:33 so I wouldn't suggest you to use  temp data if you are not sure.

  • 01:14:44 Okay, so that is the versus competition  we have data it is key but a dictionary  

  • 01:14:50 collection it is type object and  it is key but additional connection  

  • 01:14:54 and we have data is the dictionary object  and its property of controller Base Class  

  • 01:14:59 feedback is dynamic property  of controller Base Class. Okay,  

  • 01:15:04 we've data is faster than Vivek. We work is lower  than view data or so you see big data is faster.

  • 01:15:13 These data introduced introduced with a  Miss Vaughn and we see three. I miss Yvonne  

  • 01:15:20 if they'd also works with that NET  Framework 3.5 be back on works for above

  • 01:15:28 type conversion code is required  by eliminating in-depth vbac is  

  • 01:15:33 used Dynamic. So there is no  need to type conversion. Oh,

  • 01:15:36 however, if you use type conversion  

  • 01:15:41 that is that would be easier for  other coders to read your code.

  • 01:15:50 It's value becomes know if we Direction  has accurate same as me later.

  • 01:15:57 It lies on the during the  current request same as a waiter.

  • 01:16:11 Okay.

  • 01:16:17 So now we can move to next okay  before moving to next chapter.  

  • 01:16:25 Let's see what we have. Let's write what  we have seen so far in this lecture to

  • 01:16:42 Okay.

  • 01:16:48 in-depth learning

  • 01:16:54 of razors. Syntax, OK  

  • 01:16:59 and let's materialize it. Okay. You see as  I did control K Dee. It did add some empty.

  • 01:17:16 lines

  • 01:17:18 This is shorting a Bach in Visual Studio.  Okay. Okay, we have started from here.

  • 01:17:28 And let's write them also.

  • 01:17:34 if I variables in leaves

  • 01:17:40 So the accession of give is chcs HTML  C sharp HTML something like that.

  • 01:17:51 how to write mix it HTML

  • 01:17:59 and C sharp Court in we use okay.

  • 01:18:05 What else?

  • 01:18:12 let's check from our code how to write  how to do how to write or how to use  

  • 01:18:20 how to use for each if as

  • 01:18:25 for a while

  • 01:18:28 switch

  • 01:18:32 Okay.

  • 01:18:39 And give a dance.

  • 01:18:43 in razors

  • 01:18:49 in weaves

  • 01:18:54 Okay.

  • 01:18:58 how to

  • 01:19:00 how to use a custom Define its class in model

  • 01:19:13 and

  • 01:19:18 by referencing

  • 01:19:23 Model this can be applied to anything.  Actually. It doesn't have to be in under models.  

  • 01:19:32 I can just add another class  such as let's add a class.

  • 01:19:41 Let's call them as Global glasses. Okay?

  • 01:19:49 You see it is on this namespace lecture  one. I can also delete it. I can say as a  

  • 01:19:57 hobby class my or less a

  • 01:20:02 custom Global class and

  • 01:20:11 Inside it I will have integers  as I are wrong and methods.

  • 01:20:21 such as public voyage

  • 01:20:25 I'm so drunk.

  • 01:20:30 I will make his way through a private.

  • 01:20:36 And then it will return.

  • 01:20:40 something like this or cheeky

  • 01:20:41 only integers. Why do I didn't return  the wrong but let's switch on the ground.

  • 01:20:50 and

  • 01:20:53 SS public voice said Frank

  • 01:21:02 ins

  • 01:21:03 wrong, and this will set drunk which if Mission as

  • 01:21:10 equal to Long

  • 01:21:14 and something like this.

  • 01:21:18 Okay, whatever. It's not important and now we  can also use this inside our index.html which

  • 01:21:31 Using by the way, it is not under the name  space. Therefore I can edit exactly as I think.

  • 01:21:42 Asus

  • 01:21:46 yeah, it is working or

  • 01:22:03 oh, I am not able to reference it  because it is not a static class  

  • 01:22:09 therefore from the I can access it directly. Let's  check it out. It's not on the anywhere as well.  

  • 01:22:17 So I will open a code block and yeah,  it is directly available my collarbone.

  • 01:22:27 The sequel to name level classes and here  I will type it as style and font size.

  • 01:22:42 Okay.

  • 01:22:44 I started to pixels.

  • 01:22:48 And here I will call my Global classes.

  • 01:22:53 Okay, the method is not available.

  • 01:23:01 Because I have to use the custom colors custom  Global class object reference like this.

  • 01:23:16 Okay.

  • 01:23:24 Let's name this GC gloss.

  • 01:23:30 Okay.

  • 01:23:36 Yeah, I know. I just said and

  • 01:23:42 yet I said something like this and

  • 01:23:51 Okay, this is a method called our fourth.

  • 01:23:58 Does it work?

  • 01:24:02 But it doesn't work because it is expecting  

  • 01:24:08 evaluate to write inside div. Therefore. I will  set it here and then I will get it. Okay get

  • 01:24:20 Rich Little like okay, so now this should work.

  • 01:24:27 Okay, let's run it.

  • 01:24:34 Okay how to compose?

  • 01:24:40 custom glasses in an asp.net

  • 01:24:55 A hungry sea for education and it's like inside  leaves. Okay, I think is a good explanation.  

  • 01:25:09 And yeah, it is 0 the or to  punctuate method we have ridden.

  • 01:25:18 I can also change it to reach or something.

  • 01:25:36 always

  • 01:25:40 okay 0 now it will return the

  • 01:25:49 remaining of

  • 01:25:52 / 5,000

  • 01:25:59 and what else we have seen so far is

  • 01:26:05 Okay how to define local methods?

  • 01:26:12 Metals use them how to define dynamic function

  • 01:26:24 unless he Metals Metals templates  and use them inside views.

  • 01:26:37 Okay here.

  • 01:26:40 So this is ours.

  • 01:26:43 Delegate functions.

  • 01:26:48 You see functions and methods are say but  in c-sharp, they are called as method.  

  • 01:26:55 Usually therefore. I am using method.  What is the difference between

  • 01:27:04 let's say

  • 01:27:07 In data, and we've page what is the difference?

  • 01:27:14 it's being

  • 01:27:17 if data and events

  • 01:27:22 how to use beta and you back to transfer  data from controller to the we okay?

  • 01:27:38 Okay, this is working as expected.

  • 01:27:44 Alright, these are the things so far. We  have seen so we can move to the next chapter.

  • 01:27:55 By the way, this is Tom pathetic.

  • 01:28:00 and

  • 01:28:04 okay about templates. So  you see this is the template  

  • 01:28:12 it is like a local method that is formatting  and returning some data. So here's

  • 01:28:23 we are using that template to write output  as we wanted. So the output will be inside  

  • 01:28:31 div the title. So it is expecting titled. Why in  the runtime it is not checking at compile time.  

  • 01:28:41 So if I make it something like this, it will  still compile. However, it will throw an error  

  • 01:28:48 and like this this is about  formatting so the item is being

  • 01:28:52 The item is getting provided to the  Temple of the tablet is taking an object.

  • 01:29:00 And it is dynamic.

  • 01:29:03 So here this is the object. We are providing  and it is getting two objects title.  

  • 01:29:13 I mean utilizing and some I could also write  this as a method. So let's also write it  

  • 01:29:19 as a local method so it will work same and  let's name it as boy sitting format movie.

  • 01:29:35 It will take this time. It will be.

  • 01:29:41 Strong strong formatted so it will take  a type of movie Object. We are my movie.

  • 01:29:54 And it will return.

  • 01:30:00 this simply

  • 01:30:05 which one this however when we are  returning people use my movie dot I turn.

  • 01:30:22 So the title is not sitting.

  • 01:30:24 No it no it is sitting inch.

  • 01:30:29 Okay.

  • 01:30:32 What is the ever here?

  • 01:30:41 It says that.

  • 01:30:47 Cannot cover number two strings.  

  • 01:30:51 Okay. It is expecting from Us explicit  conversions. Therefore I will do it.

  • 01:31:04 Okay father movie.

  • 01:31:07 S title and release date

  • 01:31:13 so

  • 01:31:15 inside div

  • 01:31:18 we are referencing of

  • 01:31:22 local object

  • 01:31:26 Oh, I see I see so.

  • 01:31:31 When we say return sitting it says  therefore I will make it as return object.

  • 01:31:40 And now can determine object. No, I see so  this is why I have two additional formatting.

  • 01:31:51 So if you do if you write it as

  • 01:31:57 Method we it is not working as  a template. That is why we use  

  • 01:32:02 Dynamic delegates. So what I  can do at best is return this

  • 01:32:11 as a sitting and make it work I have to

  • 01:32:20 Return it like this. So basically  I have written I am not riding.

  • 01:32:29 HTML mix it code. I am riding the previous  way. This is not HTML mix it code anymore.  

  • 01:32:43 This is the sitting and it  will be needed to parse it as

  • 01:32:53 like this

  • 01:32:57 Let me show you.

  • 01:32:59 That's what way of movies so let's put it inside.

  • 01:33:09 Tiff

  • 01:33:16 Here I will call.

  • 01:33:21 Movie formats movie you see it is private because  it shows a lock here. It will take movie Object.

  • 01:33:34 And it will return this sitting  however, it will be encoded by default.  

  • 01:33:41 So let's say default and coded.  Let's see the difference.

  • 01:33:47 And here's rough.

  • 01:33:53 So to have it raw I have to  use HTML now methods like this.

  • 01:34:02 Okay.

  • 01:34:11 All right. So let's run the application.

  • 01:34:34 Okay, so then we are using default and  coded your you see it is printed as  

  • 01:34:42 a sitting a text. Therefore. It is  not HTML formatted. The format is not  

  • 01:34:49 working. However when we use as HTML raw,  you see it is printed as we wanted, okay?

  • 01:35:03 Jesh so

  • 01:35:10 what is it?

  • 01:35:18 Delegate templates.

  • 01:35:25 printing easily HTML formatted data

  • 01:35:32 Yeah, isn't it is more explanatory?

  • 01:35:44 Okay, this is this is the everything now  you see the difference between methods and  

  • 01:35:53 dynamic select template.

  • 01:35:57 completed delegate by the way and plated

  • 01:36:25 Okay.

  • 01:36:27 So we can move to do next  chapter. I will take a pause here.

  • 01:36:36 Okay. Now we are starting to see controllers  in more depth. So introduction to controllers.

  • 01:36:45 In the beginning of this tutorial we briefly  talked about controllers in the MVC pattern and we  

  • 01:36:50 even added one to our project to see what it looks  like and what it is to get you quickly up and  

  • 01:36:55 running in this chapter. We will go more in depth  with the topic of controllers starting in this  

  • 01:37:01 article where we will go over the Core Concepts  of a controller. Okay. So what is controllers?

  • 01:37:08 As mentioned in the start of this tutorial  the controller acts as the middleman  

  • 01:37:12 it will combine your model with a view and serve  the result to the end user. However, neither a  

  • 01:37:18 model nor view is required. The controller can  act on its own for the most basic operations.  

  • 01:37:24 EG delivering a simple text message or redirecting  the user to somewhere else. We'll discuss this in  

  • 01:37:30 the later article on action results. So you so you  can do simple operations with controller. However,

  • 01:37:39 What we are targeting is an advanced  setup application a web application.

  • 01:37:45 In asp.net MVC a controller is just like  any other class. So it has a CSS file  

  • 01:37:51 extension or VB if you use Visual Basic  and looks like any other dotnet class,  

  • 01:37:57 however, there are a few things that  will allow you and the.net framework to  

  • 01:38:02 recognize it as an MVC controller. Okay,  so by default it is not recognized as an  

  • 01:38:07 MVC controller. So there are few things that we  will let us to Define it as an MVC controller.

  • 01:38:17 It's usually placed in a folder called  controllers in the root of your project  

  • 01:38:21 it inherits from Microsoft ASP net core MVC  controller or from one of your own classes,  

  • 01:38:27 which then inherits the Microsoft ASP net core  MVC controller class. The name of the class  

  • 01:38:33 will usually end with the word controller. EG home  controller or products controller. Okay. These are  

  • 01:38:40 these may not be necessary. However than  that would require you to do extra things.

  • 01:38:46 So you should follow the coding patterns in  MVC especially so when we check our controller,  

  • 01:38:55 we can see that it is inheriting controller class  from because of asp.net core MVC controller.  

  • 01:39:03 So this is not just a regular class. It is their  children of controller class. It is inheriting it.

  • 01:39:17 If you don't follow these conventions  the.net framework will not be able to  

  • 01:39:21 recognize your class as a controller. So  it makes sense to follow them. However,  

  • 01:39:26 if you insist on EG naming your controller  classes differently, you can decorate it  

  • 01:39:31 with the controller attribute placed right  before the class declaration. So I used if you  

  • 01:39:36 change the name of your controller than you  need to decorate it with controller attributes.

  • 01:39:42 But I don't suggest that.

  • 01:39:46 By inheriting the Microsoft ASP net core  MVC controller class you get some added  

  • 01:39:51 functionality that you can use for MVC purposes.  EG the ability to return views partial views.  

  • 01:39:58 It also allows your controller class to access  HTTP related information like the query string  

  • 01:40:04 thanks to the HTTP context property on the  controller class. In other words. It turns a  

  • 01:40:10 regular dotnet class into a web aware class  allowing you to do stuff you would normally

  • 01:40:15 Able to do in your PHP or ASP classic file or  any of the many other web Technologies out there.  

  • 01:40:22 So if you are riding a class for as a controller  of her via, then you should inherit always the  

  • 01:40:31 controller's class of because of the is dotted  quarter and we see what you saw where the controls  

  • 01:40:38 are play sitar. They are in place it inside  controllers folder and they are ending with a

  • 01:40:45 roller board as a suffix

  • 01:40:49 So this is about how controllers are Define  it and name it. Let's move to the next.

  • 01:40:58 Okay, then the actions are coming. So in  our controller, we already have an action  

  • 01:41:06 which is I action result index. So when  our application is running basically  

  • 01:41:13 it is looking for an action named as indexed  by default. Okay, it is how it is working  

  • 01:41:20 so we can Define multiple actions multiple  actions and they will be added as an

  • 01:41:30 Suffix to the URL you will see we will  see about that. So let's continue.

  • 01:41:37 Since a controller is just a regular dotnet  class. It can have Fields properties and methods  

  • 01:41:43 especially the methods of a controller  class are interesting because they are  

  • 01:41:47 the connection between the browser and therefore  the user and your application for that reason.  

  • 01:41:52 The methods of a controller class is referred  to as actions a method usually corresponds to  

  • 01:41:58 an action in your application which then  returns something to the browser user.  

  • 01:42:03 Okay. This is an extremely convenient features.

  • 01:42:07 That does not exist in the asp.net web forms,  or as far as I know in other applications.  

  • 01:42:16 I don't know. I'm not sure about that though. So  this is a great features and we will see that so  

  • 01:42:24 this is important. It is a dear connection between  the browser and the user and your application.

  • 01:42:32 Since a browser works by calling a URL  you need something that translates URLs  

  • 01:42:37 to a corresponding controller in action method  

  • 01:42:41 for instance. The browser might request a  URL like products one and then you want your  

  • 01:42:46 products controller to handle this request  with a method action called details. This is  

  • 01:42:51 done with the concept of routing something. We'll  talk much more about elsewhere in this tutorial,  

  • 01:42:56 but for now just know that routing is what  connects URLs to actions on your controllers.

  • 01:43:02 Okay, and let's continue.

  • 01:43:10 When creating your controllers keep in mind  that all public methods on a controller class  

  • 01:43:15 is considered an action. This means that if  you have defined catch-all routing rules for  

  • 01:43:20 your controller and that is a common thing  to do all the methods in your controller  

  • 01:43:24 class can in theory be reached using a URL.  So if you have methods on your controller  

  • 01:43:30 that you don't want the end user to be able  to call be sure to mark it as private as an  

  • 01:43:35 alternative if you really need a method  to be public but not accessible by URL.

  • 01:43:40 In Mark the method with the non action attribute.  Okay. So this is important whatever I write  

  • 01:43:46 here as a public can be accessed with URL and to  prevent it. I in May I have to make it as private  

  • 01:43:56 or Market as non action attribute non-action  attribute is important to keep that in mind.  

  • 01:44:03 So you have ridden hidden method here.  However, it can be discovered from

  • 01:44:10 Found with malicious users  and can be abused against you.  

  • 01:44:17 Therefore always keep in mind that every  user can be malicious. Okay can be a hacker.

  • 01:44:25 action verbs

  • 01:44:29 to gain more control of how your actions are  called. You can decorate them with the so-called  

  • 01:44:33 action verbs. These are in fact regular dotnet  attributes, which will tell the dotnet framework  

  • 01:44:39 how an action can be accessed without these  attributes and action can be accessed using all  

  • 01:44:45 possible HTTP methods the most common ones are get  in post, but you can change that pretty easily.  

  • 01:44:52 Okay, so this action can be accessed  with only HTTP get there for you.

  • 01:44:58 Won't be able to access this method  with post methods or other things.

  • 01:45:06 this defines that

  • 01:45:10 now the edit action can only  be accessed with a get request.  

  • 01:45:14 This has the added benefit of allowing you  to have multiple methods with the same name  

  • 01:45:18 as long as they don't accept  the same request method.  

  • 01:45:22 So for instance, you could have two methods called  edit. The first one would be available for get  

  • 01:45:26 requests and generate the form for editing an item  while the second one would only be available for  

  • 01:45:32 post requests and be used to update the item  when the form was posted back to the server.

  • 01:45:37 It could look like this. So you see this is  HTTP get is he stupid post when you get it  

  • 01:45:44 will return the form when you post with the  same name, it will update the item on the  

  • 01:45:54 let's say server or whatever you want to do. Okay.

  • 01:46:03 So maybe we can start with making example of this.

  • 01:46:09 To understand it better. So  inside our home controller.  

  • 01:46:14 I will define an edit and  edit will return will however

  • 01:46:24 I will set.

  • 01:46:29 a different title and release date

  • 01:46:32 for this one

  • 01:46:39 either way, I have to copy this and

  • 01:46:47 to make our index.html work

  • 01:46:51 and let's say editing window will  be the title and date is important  

  • 01:47:00 and on the post post will not  open string also return but it  

  • 01:47:06 will return this time product updated. So  it will take a product instead of that.

  • 01:47:18 I think I will just return a city.

  • 01:47:25 product update is bitch text and

  • 01:47:32 the text you post

  • 01:47:35 ridden

  • 01:47:40 Okay, so to access this I will  just append edit to the end of the  

  • 01:47:47 URL and to access this I have to make  a post HTTP post to edit URL. Okay.

  • 01:48:13 Okay, then let me type like this.

  • 01:48:19 This is not working because  probably we didn't add roasting yet.

  • 01:48:34 It's not working.

  • 01:48:38 To make it work. We need to add Roti and we  haven't added yet. So let's just stick to our

  • 01:48:48 toriel

  • 01:48:54 oh, I think I can already add it from our

  • 01:48:59 programs here is probably I  can't remember. How was it?

  • 01:49:09 Yeah, I can't remember so.

  • 01:49:22 Let's just let me just remember it or he is.

  • 01:49:34 Why do you need to rotate it?

  • 01:49:50 nice try

  • 01:49:59 by default we could also add but  he didn't add the defaults yet.

  • 01:50:09 He's was the way about me.

  • 01:50:17 this is the force for Change and

  • 01:50:25 did you edit was not formed?

  • 01:50:28 Or it is looking for edit CSS HTML.

  • 01:50:44 Why?

  • 01:50:47 J arms

  • 01:50:54 Oh, I remember I remember.

  • 01:51:02 This is index HTML de force.

  • 01:51:06 To access these methods. I remember correctly.

  • 01:51:16 I have to write full paths.

  • 01:51:20 Like here's index.

  • 01:51:24 and it's

  • 01:51:37 my home your home is working but

  • 01:51:42 It is not why?

  • 01:51:51 Okay.

  • 01:52:04 I  

  • 01:52:34 need to

  • 01:52:38 change the default.

  • 01:52:47 I can't remember. How was it?

  • 01:52:52 Okay, let's just continue to

  • 01:52:54 Kotori about I wanted to build  your house. Does this look okay?

  • 01:53:09 I didn't have it folks.

  • 01:53:34 So I have to add a road map like this.

  • 01:53:45 so here in program CS

  • 01:53:48 or in start-up at this and here.

  • 01:53:56 Okay.

  • 01:53:59 It is not using MVC roads yet.

  • 01:54:03 Darfur

  • 01:54:15 Okay, I have to use I have to delete this.

  • 01:54:22 mom

  • 01:54:44 and research has enabled and funks working force.

  • 01:55:05 So I have to okay, I have to go watch it.

  • 01:55:09 I'll shoot fuck. Yeah.

  • 01:55:31 look

  • 01:55:39 anyway, I will just

  • 01:55:43 I said for this time because it will see in India.

  • 01:55:49 following

  • 01:55:54 maybe this box. Let's see.

  • 01:56:06 I will just yeah, do not break our application.

  • 01:56:23 change

  • 01:57:18 No.

  • 01:57:20 I can.

  • 01:57:23 Write another one.

  • 01:57:26 Let's say the fall to home action will be edit.  He won't be taking ID, but we can keep it.

  • 01:57:53 Okay, it is working and

  • 01:57:58 If it is not working make it back home.

  • 01:58:04 Okay.

  • 01:58:13 Control over the hump.

  • 01:58:15 Okay, this is not the  

  • 01:58:24 time.

  • 01:58:36 controller home action edit wine habits

  • 01:58:45 But it is expecting with I see just  working but is expecting a different wave  

  • 01:58:53 because it is different. Therefore.  I have to return give with the name  

  • 01:59:00 of the wave as well. So the VM name will be

  • 01:59:05 Index we should do. It was also working previously  

  • 01:59:14 with our important roles in but he  didn't return the collective name.

  • 01:59:31 Yeah now working so I would convert this.

  • 01:59:42 pick

  • 01:59:43 what did you know?

  • 01:59:48 And now we should Park.

  • 01:59:51 Okay, maybe default control the root.  So controller home action index.  

  • 01:59:56 So whatever we add it will be by default  names search it for I will show you what does  

  • 02:00:04 that mean? So by default it will be  looking for Home in days equal to this.

  • 02:00:12 You see now I can use from edit and it will  be working because it will be looking for  

  • 02:00:21 edit. And now I can also do a post but  I will show the post methods in the

  • 02:00:32 let's do it right now.

  • 02:00:44 I'm just I just need.

  • 02:00:49 the postal here

  • 02:00:56 Okay.

  • 02:01:02 We will see all of these in the following  lectures with the following story of words. I  

  • 02:01:08 just wanted to make that more clear. So let's  remove all these so input our values submits.

  • 02:01:28 Yeah, just let's say post example, so  this will post to hold controllers.

  • 02:01:38 However, the action name will be at it's  okay. So the format is going to post  

  • 02:01:44 and then I post example are it  will expect from me an object.

  • 02:01:55 Which object is filled expect

  • 02:01:59 is a bicycle it will be the value  of problem in this check it out.

  • 02:02:03 So home, I mean when I click this.

  • 02:02:10 Product updated with text or text is not  posted store. So I need to append the post.

  • 02:02:19 volume so

  • 02:02:27 we were a pending to post which you can  open it. It's hidden values, I think.

  • 02:02:33 so

  • 02:02:48 nothing is born.

  • 02:02:54 I think I need to apply an input type  while you a or something like that.

  • 02:03:00 input type, okay, but other types that are

  • 02:03:05 equal to

  • 02:03:15 yeah, let's make it hidden.

  • 02:03:23 I need a name of input. So  I will Define the name as

  • 02:03:28 our text which is matching the name inside my  home controller here. So the value will be our  

  • 02:03:40 first method action example, okay at the code  behind that the server side it will get it. So  

  • 02:03:51 I am going to put a break point here to show  you by the way. This will only work when we do.

  • 02:03:59 Post then we get it will enter  this and it's sparking already.  

  • 02:04:05 So I will put a breakpoint here too as  well to show you. Okay, then home edit.

  • 02:04:13 You see it's answering here as expected  

  • 02:04:16 side edit method and then you see the  title is editing window when I click post.

  • 02:04:26 Okay, it is working as our Tech's our first method  example and it is working as expected. Okay,  

  • 02:04:32 so these are about actions and if you return a  different name with action, it is expecting you  

  • 02:04:41 to have same name. It's view because of default  routing or you can Define the return we've named.  

  • 02:04:50 So let's also write them.

  • 02:04:53 Let me see all those odds are  actions and we see others.

  • 02:05:10 How does default protein matches?

  • 02:05:19 controllers actions

  • 02:05:27 action time such as HTTP post and

  • 02:05:38 HDPE gets too cold actions  inside miss you controllers.

  • 02:05:50 Okay. Okay. I think this is enough for this  week. So hopefully see you next Stitch. This  

  • 02:05:59 is the end of the lecture to if you have any  questions, please ask me from our discourse page  

  • 02:06:07 and you can also email me  but this court is better. So  

  • 02:06:12 other your friends also can see them  other citizens can also see them and

  • 02:06:24 All right, end of lecture to  hope to see you next week.

Clone this wiki locally