(#10) How to write C# Code in MVC View? | C# code in View | ASP MVC .NET 6 C# Tutorial for beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys how are you i am new today i decided to record a video about how we can use the c sharp code inside the view in spmc.net 6 application so let's restart i will go to the visual studio 22 version and i will create a new project so let me run the visual studio as an administrator here i will select this template as i am always doing and i will search here as pmvc guys i will tell you that i am creating always a new project in each video except starting from the previous uh from the previous video project because i uh let's suppose uh a student or anybody comes directly to this video then there will be no need uh for him or her to see the previous video also because uh in the previous video i may repeat some other kind of steps and this video some other kind that is why i want to create each and everything from the scratch in each video so i will select this option click on next so here i will select sp mvc to to do app one yes it will be the project name and my project will be saved inside this location i will click on next and here i will select the dot net 6 template uh sorry framework and from that from the authentication tab i will select none and just uncheck this one you can also check it's not a problem click on create because of this video how we can use c sharp code inside the view of spmc.net 6 application right now the project created successfully no i will show you steps inside the home page of the website as you can see that there are some kind of ready made code inside the project so i will show you uh let me add one another controller okay it will be i think a better view a better way to add an a new controller controller uh the controller no let's suppose should be student controller dot cs yes and inside the con student controller i want to add a method public i action result [Music] students list yes here i will return a view okay keep in mind that it is a good practice to put the http get attribute and view you can also put the http post another kind of filters but i am using these action method for the view that is why i will put the http get right now here for this controller for the student controller i will add here a view folder add folder and the folder name should be sorry student it will be the folder name and here inside this folder let's me add a view for this for the student list so right click add view select this option reserve you click on add and then give here the name to the view it should be student list student list and then select here the layout page go to use folder go to shared and select this layout click on add the view edits successfully then we will go directly to the view and we will write c sharp code inside the view okay guys as you can see that the view is added successfully now i will add here some kind of c sharp code first lets me add a very simple h2 tag it is students list yes let me run the application click on this option to run the project i will redirect to the student list view the student list view is inside the student controller so i will copy this option yes this one and then the view name the view name is student list so the full url is student the controller name and the view name okay now i am at the student list page right oh in the view no i will add a c sharp code for adding the c sharp code you have to write the address symbol and then curly braces the address symbol and the curly braces no inside these i can i can use the c sharp code uh let's make a very simple variable like student yes and the student name should be like nude now i am using uh directly the c sharp code inside the view and let's suppose its id should be 555 the student id and id yes now these two are the lines of the c sharp code now i can use this variable no inside the sorry guys it should be integer yes now i will use these two variable inside the normal html like in this way let's make a span then m is id is equal to add the rate student id now to access the c sharp variable from your html you have to write again the add the red symbol if i just remove this one then this string will be printed except the value of the student id so i am axis like in this way again i will put i will put a break and then in the next pane copy this one paste here i will print here the name of the student this one let's me hardly load the page you can see that the student id is 555 and the student name is node let me add here a very simple function like public int get student i id like in this way here i will return it should be of string type and here i will return a simple thing like nor in logic okay no it's giving me the modifier public is not valid at this point yes i think i should use like in this public static string and our the access modifier may not work in the view okay right now i will use this function inside of for the student name add the rate yes like in this way now this line of code will print basically the null because when the control comes at this point this function will be headed and it will return more let me also put here a debugger a debugger sorry a debugger at this point why it's not working not working no issue let me remove all these things and run the application again student list so it is student and student list you can see that the new is printed again now if i just change the function a little later append student id with student name and i will return here like in this way student name i want to append the student id with the with the student name plus i will add an underscore symbol and then i will append the student id student id dot to string because it is an integer and i i will convert into to string and then will append this line of code a static local function cannot contain a reference student name let me copy these two and place inside these yes not work yes it's working and here i will put here adami student id at this time just copy this one and paste here now i am calling the c sharp function from the html this function and this function will append the student name with the id and in the middle there will be the underscore sign let me hardly load the page you can see that the student id and name is appended and there is underscore sign in the middle so guys you you observe the changes and the things and the usage that how we can use the c sharp code inside the html inside the view in c sharp i mean in spmvc.net six thank you for watching the video thank you and if possible please subscribe the channel thanks
Info
Channel: Noor Codelogics
Views: 644
Rating: undefined out of 5
Keywords: how to write c# code in mvc view, cshtml c# code, razor c# code, write c# code in view, c# code in razor view, Razor view in MVC, Razor Pages, razor pages in asp.net core, c# code in razor view .net 6, c# code in view .net 6, c# code in asp mvc .net 6 razor view, ASP MVC .net 6 razor view, .net 6 razor view, code in .net6 view, c# code in .net 6 view, c# code in view asp mvc, c# code in asp mvc
Id: H6FNrUM0F8o
Channel Id: undefined
Length: 12min 54sec (774 seconds)
Published: Thu Sep 01 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.