Creating Controller
First of all, you need to add a code file in your project.In the file you just created add this code:
public class ProductController : Controller
{
public ActionResult Index()
{
//Add action logic here
return View();
}
}
public class ProductController : Controller
{
public ActionResult Index(int parameter1, string parameter2)
{
//Add action logic here
return View();
}
}
Color Coded (above) Explanation:
- RED: The Controller Name. In making a controller you must keep this format, ControllerName + Controller.Examples:
Controller Name Controller Class Name Profile ProfileController Department DepartmentController Request RequestController You should also remember that if you create a view on this controller you must create a new folder under the views with the same name of your Controller Name. - Green: The Action Method. This is the method in which your logic will be implemented. As default, Action Method Name (with return value of ActionResult) should be always the same as the ViewPage Name you want to render. If you want to render a ViewPage that is not the same as Action Method's Name, then you should specify it in the View() Method.
EXAMPLE:
return View("AnotherViewPage");
(Click this for the Reference) - BLUE: Action Method return value.
ActionResult Return Type
All action methods must return an instance a class that is derived from ActionResult. The ActionResult class is the base for all action results. However, there are different action result types, depending on the action that the action method is taking. For example, the most common action is to call the View method. The View method returns an instance of the ViewResult class, which is derived from ActionResult.
The following list shows the built-in action result types.
ViewResult. Returned by the View method.
RedirectToRouteResult. Returned by the RedirectToAction and RedirectToRoute methods.
RedirectResult. Returned by the Redirect method.
ContentResult. Returned by the Content method.
JsonResult. Return by the Json method.
EmptyResult. Returned if the action method must return a null result.
No comments:
Post a Comment