Tuesday, 22 March 2022

Views in Asp.net core MVC

 # Views in Asp.net core MVC

1. return View()
    - asp.net mvc core looks for the view with name equal to 'action name'
       public ViewResult Details()
        {
            return View();
        }
        In this example it will try to load Details View in folder <Controller>Views\<Action>name
2. View(string viewName) method
    - It will try to load view with the name 'viewName'
    return View("Test");
    - it will look for view 'Test.cshtml'
3. Specifying view file path
    - return View("MyViews/Test.cshtml");
4. Relative View File Path
    - return View("../Test/Update");
5. View(object model) pass model data
6. View(string viewName, object model)     Pass both the view name and model data.

## Passing data to view in ASP.NET Core MVC

1. there are 3 ways to pass data from a controller to a view
    - Using ViewData - loosely typed view
    - Using ViewBag - loosely typed view
    - Using a strongly typed model object. This is also called Strongly typed view.

## Using ViewData to pass Data from a Controller to a View

1. in controller  ViewData["PageTitle"] = "Employee Details";
2. Accessing ViewData in a View  
    -  <h3>@ViewData["PageTitle"]</h3>


No comments:

Post a Comment

Search This Blog

Creating your first "Alexa" Skill

Index What is Alexa What is Alexa Skill? Why is it required when Alexa already equipped with voice assistant? Dev...