ASP.NET Core 6.0 MVC Tutorial Day 4
Learning Objective: Now Let’s discuss about Redirect, Razor Templates to pass value using Model and pass array of values and many other concepts.
public IActionResult ViewMore()
{
return Redirect("http://google.com");
}
public RedirectToActionResult ViewCourses()
{
return RedirectToAction("Courses");
}
Let’s create model, Let’s create object of the same in controller and pass it to view.
Models >> Course.cs
namespace WebApplication1.Models
{
public class Course
{
public string coursename { get; set; }
public string coursedesc { get; set; }
public int coursefees { get; set; }
}
}
Controller Code
public IActionResult ViewASPCourseDetail(string coursename)
{
Course c = new Course();
c.coursename = "ASP.NET Core";
c.coursedesc = "Learn the complete asp.net core from fundamental to advanced level";
c.coursefees = 5000;
return View("Courses", c);
}
View Code
@model WebApplication1.Models.Course
@{
Layout = null;
}
<div class="text-center">
<h1 class="display-4">Courses</h1>
<h2> @Model.coursename - Rs.@Model.coursefees/- </h2>
<h3> @Model.coursedesc </h3>
</div>
Let’s Pass multiple items / Array of items
public IActionResult CourseList()
{
Course c1 = new Course();
c1.coursename = "ASP.NET Core";
c1.coursedesc = "Learn the complete asp.net core from fundamental to advanced level";
c1.coursefees = 5000;
Course c2 = new Course();
c2.coursename = "Web Development";
c2.coursedesc = "Learn Web Development from the Scract including Frontend and backend";
c2.coursefees = 5000;
Course c3 = new Course();
c3.coursename = "Mobile App Development";
c3.coursedesc = "Learn Mobile App Development with Deployment ";
c3.coursefees = 5000;
Course[] courselists = { c1, c2, c3};
return View("Courses", courselists);
}
View Code
@model WebApplication1.Models.Course[]
@{
Layout = null;
}
<div class="text-center">
<table>
<tr>
<th> Course Name </th>
<th> Course Description </th>
<th> Course Fees </th>
</tr>
@foreach (Course c in Model)
{
<tr>
<td> @c.coursename </td>
<td> @c.coursedesc </td>
<td> @c.coursefees </td>
</tr>
}
</table>
</div>