LICT .NET Training | Kamal Protap Sir | Part 4

MVC Part:

Controller getting input.

ApplicationManagement video was so important

 

In Controller Folder HomeController.Cs is a controller which is controlling all the things

here is the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MVC.Controllers
{
    public class HomeController : Controller
    {
        public string Index() //we can override the abstract class ActionResult is an Abstract class
        {
            return "hi welcome";
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }
}
}

App_Start Folder:

RouteConfig.cs file we will find that

using System.Web;

namespace MVC
{

public class RouteConfig{

public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathinfo}");

routes.MapRoute(
name:"Default",
url: "{controller}/{action}/{id}",
defaults:new{controller="Home",action="Index",id=UrlParameter.Optional
);
}
}
}

Views/Home/About
Views/Home/Contact

How to communicate with Controller and View:
ViewData and ViewBag passing the data from a controller to view

Interview questions:
ViewData: ViewData is dictionary of objects that is dervied from ViewData dictionary class and is accessible using string as keys.

ViewBag: viewBag is dynamic property. That take advantages of dynamic features.

both does not provide any compile time error.

 

In model we need to write the business logic.

In View we can show the actual things what we want to show:

View->Details.cshtml

@model MVC.Models.Employee
@{
    ViewBag.Title = "Details";
}

<h2>Employee Details</h2>
<table style="font-family: Arial">
    <tr>
        <td>
            <b>Employee Id:</b>
        </td>
        <td>
            @Model.EmployeeId
        </td>
    </tr>
    <tr>
        <td>
            <b>Employee Name:</b>
        </td>
        <td>
            @Model.Name
        </td>
    </tr>
    <tr>
        <td>
            
        </td>
    </tr>
     
</table>

Employee.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MVC.Models
{
    public class Employee
    {
        public int EmployeeId { get; set; }
        public string Name { get; set; }
        public string Gender { get; set; }
        public string City { get; set; }
    }
}

EmployeeController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVC.Models;

namespace MVC.Controllers
{
    public class EmployeeController:Controller
    {
        public ActionResult Details()
        {
            Employee emp=new Employee()
            {
                EmployeeId=1,
                Name="Kamal"
                Gender="Male",
                City="Chennai"
            };
            return View(emp);
        }


    }
}

 

 

 

It would be a great help, if you support by sharing :)
Author: zakilive

Leave a Reply

Your email address will not be published. Required fields are marked *