MVC-Chapter2
MVC-Chapter2
----------
Working with Controllers and Views:
Model refers to the set of classes that describes the data that the
application works with.
View is a combination of HTML mark up and code that runs on the web
server .
Controller refers to the set of classes that handle communication from
the user and overall application flow.
MVC provides a Scaffolding technique feature that provides a quick
way to generate code automatically for common used operations
Bundling is a technique provided by ASP.NET MVC that allows you to
combine multiple files such as CSS and JS
The mechanism to locate an appropriate action method for a given URL
is called Routing. In ASP MVC routing has 2 purposes.
It maps the URLs of the incoming requests with controller actions.
It builds outgoing URLs that correspond to controller actions.
Controller:
-- Process incoming request.
-- Executes the appropriate code initiated or required by the user
-- Communicating with the "Model" to validate and process the data.
-- Rendering the required view.
---Handles all incoming requests.
---Controllers are implemented in MVC application as C# classes
inherited from System.Web.Mvc.Controller class
Controller - is a class...
it is .cs code [C# file], which is inherited by a built-in Controller classes.
Create the class (.cs file) in the folder Controller.
public class HomeController : Controller
{
//code
}
Note: the name of the controller should be suffixed with Controller
keyword. - Naming convention.
Inside controller, we can create action methods.
These action methods are public methods that will be invoked in
response to the user actions.
public class HomeController : Controller
{
public ActionResult Index()
{
//code
}
}
Note: The method name can be anything, but return type of the method
should be ActionResult which is an instance of
System.Web.Mvc.ActionResult class.
Most Action methods return ActionResult instance but other return types
like int, string or bool also can be used.
public class HomeController : Controller
{
public string Index()
{
Return “Home controller Index method”;
}
}
http://localhost:1078/Home/Index
Example 2:
public string Display()
{
return "From display";
}
To invoke
http://localhost:1078/Home/display
(localhost:1078 is automatically generated by your computer. It will vary
everytime.)
Passing Parameters in Controller Actions
QueryStrings --- is a part of a URL. It is is used to pass data to web
servers and starts with ?ParamterName=Value
http://localhost:77701/ Home/Browse?Category=Soap
In the above url, Browse is the Action method and Category is the
parameter. So our Action method will be
public string Browse(string Category)
{
return ("Category is " + Category);
}
To invoke
http://localhost:1078/Home/Browse?category=mvc
Integer return
public int BrowseInt(int Category)
{
return (Category);
}
http://localhost:1078/Home/BrowseInt?Category=8
output : 8
For passing multiple parameters
public string DisplayNew(string cat,int pno)
{
return "Category is "+ cat + " pno is " + pno.ToString();
}
To Invoke
http://localhost:1078/Home/DisplayNew?cat=try&&pno=7
output: Category is try pno is 7
http://localhost:59265/Employee/Index
(it will open the NewView view)
Example 2:
public ActionResult display()
{
return View("ShowData");
}
------------------------------------------
Note : To access the View
http://localhost:59265/Employee/display
(it will open the ShowData view)
In order to render a different view from a different folder, specify the full
path of the different view as a parameter of the View() method.
Add the below code in HomeController (Another action method)
Create a folder Prod in Views folder in your application
Right click Views folder and add View and give the name ProdTest
Type the following code
p> this is product test view</p> in the ProdTest
http://localhost:1078/home/Openindex
output: ProdTest
Demo 03:
-------------
File -> new project-> Choose Template Web-> Choose
ASP.NET MVC4 Application -> Choose Basic application->Click OK
Add a new controller -> EmployeeController
public class EmployeeController : Controller
{
//
// GET: /Employee/
public ActionResult Index()
{
return View();
}
}
Select the index method-> right click and add a view.
By default in solution explorer-> inside View Foler-> Employee Folder
would have got created and then index.cshtml
file would have got stored.
By default, in the routeconfig file, the name of the controller is
Home. Change that to Employee as given below.
defaults: new { controller = "Employee", action = "Index", id =
UrlParameter.Optional }
Now, add one more method to the controller.
public class EmployeeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult accept()
{
return View();
}
}
Same as click on accept-> add a view.Its gets stored in the employee
Folder.
Run the demo by default, index page appears.To view the accept page,
Modify the URL as
http://localhost:59265/Employee/accept
-----------------------------------------
<p>@ViewData["k1"]</p>
<p>@ViewData["k2"]</p>
To access, index.cshtml
<h1>@ViewBag.EmpID</h1>
<h1>@ViewBag.EmpName</h1>
<h1>@ViewBag.Department</h1>
Difference between viewdata and viewbag is that
viewdata can handle only one data at a time,whereas viewbag has the
ability to store more than one data
--- View follows an engine called RAZOR
--- Razor is a markup language [ Combination of HTML markup code +
C# code]
-- Extension will be .cshtml or .vbhtml
The Razor syntax is based on the C# programming language, and that's
the language that's used most often with ASP.NET Web Pages.
In Razor you can create blocks of code nearly anywhere using @{ }.
Rules:
1.@Character to be used while using Serverside[C# or VB]
2. Statements should end with Semicolon
3. Variables to be declared using var keyword
4. Strings to be used with double quotes
5. C# code are case - sensitive.
The Razor code is written Views that is inside a cshtml or vbhtml.
Eg:
Comments
Comments in Razor can start with @* and end with *@. Nothing inside a
comment will execute or appear in the output, not even HTML markup.
@* my comments *@
Now , the above view can be used in any of the view pages
using the following code:
@Html.Partial("_MyView");
Routing Requests to Controller Actions