Redirect action using Redirect Result in ASP.NET MVC

The RedirectToAction() method can be used to return a RedirectResult that redirects a user from one controller action to another. using System.Web.Mvc; namespace MvcApplication1.Controllers { public class ProductsController : Controller { public ActionResult Index() { return View(); }   public ActionResult Details(int id) { if (!id.HasValue) return RedirectToAction(“Index”); //return View(); } } } We can […]
Continue reading…

Enjoyed this post? Share it!

 
 

Explicitly return a view name other than default

The name of the view is specified explicitly in the example below. The ASP.NET MVC framework determines the name of the view from the name of the action if it is not mentioned. using System.Web.Mvc; namespace MvcApplication1.Controllers { public class ProductsController : Controller { public ActionResult Details() { return View("ProductDetails); } } } In this […]
Continue reading…

Enjoyed this post? Share it!

 
 

Return a View Result in ASP.NET MVC

A ViewResult is the most common ActionResult returned by a controller action. A ViewResult in ASP.NET MVC represents a view. ViewResult is returned when you want to return HTML to the browser. Example: using System.Web.Mvc; namespace MvcApplication1.Controllers { public class ProductsController : Controller { public ActionResult Index() { return View(); } } } The view […]
Continue reading…

Enjoyed this post? Share it!

 
 

Types of ActionResults in ASP.NET MVC

ViewResult— ASP.NET MVC view. PartialViewResult— Fragment of an ASP.NET MVC view. RedirectResult— Redirection to another controller action or URL. ContentResult— Raw content sent to the browser. JsonResult— JavaScript Object Notation result. FileResult— File to be downloaded. EmptyResult— No result returned by an action. HttpUnauthorizedResult— HTTP Unauthorized status code. JavaScriptResult— JavaScript file. RedirectToRouteResult— Redirection to another […]
Continue reading…

Enjoyed this post? Share it!

 
 

Pass parameters to actions in ASP.NET MVC

The routeValues parameter for Html.ActionLink() helper can be used to pass parameters to the invoked hyperlink / action. Example: <%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>   <asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server"> <p> Click to update record: <%= Html.ActionLink("Update Item", "Update", new {Id=4}) </p> </asp:Content> The generated HTML output will be <a href="/Home/Update/4">Update Item</a> Route values are […]
Continue reading…

Enjoyed this post? Share it!

 
 

Display links using ActionLink() helper in ASP.NET MVC

HTML helpers in a view are used to render HTML content. They return a string generally. Hyperlinks in a view can be generated using the HTML.ActionLink() helper. Example: <%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" ➥Inherits="System.Web.Mvc.ViewPage" %>   <asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server"> <p> This link is created by ActionLink() helper:   <%= Html.ActionLink("click Me", "Somepage" ) %> </p> […]
Continue reading…

Enjoyed this post? Share it!

 
 

Get name of current controller and action in MVC

Use this.RouteData.Values(“action”) and this.RouteData.Values(“controller”) for getting name of current controller and action in MVC instead of going through the ValueProvider. The RouteData contains information on the request / route (including controller and action), and the value provider contains information used for binding. Example of ValueProvider: string controllername = this.ValueProvider.GetValue("controller").RawValue.ToString(); string actionname = this.ValueProvider.GetValue("action").RawValue.ToString();
Continue reading…

Enjoyed this post? Share it!

 
 

_remap function in codeigniter

In codeigniter,by default, the index() function is called if no other function is passed in the URL. Developers can alter this behaviour by including a function called _remap($function). Where $function is the function, in the controller to intercept and redirect. The function _remap always gets called first in a controller irrespective of URL request. For […]
Continue reading…

Enjoyed this post? Share it!

 
 

Default controller in codeigniter

In the default installation of codeigniter, when we navigate to our localhost or domain.tld, the welcome controller is invoked. By default, the welcome controller is the controller to be invoked at the root of the website. It is possible to change the default controller to other user created controllers using the /application/config/routes.php file. $route[’default_controller’] = […]
Continue reading…

Enjoyed this post? Share it!

 
 

Learn Codeigniter PHP Framework : Part 8 – codeigniter database access using active record

Codeigniter implements the active record design pattern which allows developers to easily handle databases. It makes the database access layer simple to use and maintain. Active record represents the tables in a database as an object. Typically, each table is a class, and each single row becomes an object. All operations such as create, read, […]
Continue reading…

Enjoyed this post? Share it!

 
 

Learn Codeigniter PHP Framework : Part 7 – Passing parameters to codeigniter controller functions

Let us see how to send parameters to a function in a controller using GET. GET parameters are always visible from the Url.Example: http://localhost/index.php/welcome/greet/mani. In codeigniter parameters in query string such as index.php?name=mani are disabled using the config.php. All parameters are passed as URI elements preceded by a forward slash. welcome is the controller, greet […]
Continue reading…

Enjoyed this post? Share it!

 
 

Learn Codeigniter PHP Framework : Part 6 – Codeigniter views

Views are HTML files that can contain PHP code generally. They are loaded by $this->load->view(‘viewname’, $data). Views are loaded and used in the same function in a controller. The view is written in HTML. The PHP code is included within tags as with any HTML file. It is saved as a .php file in the […]
Continue reading…

Enjoyed this post? Share it!

 
 

Learn Codeigniter PHP Framework : Part 5 – Codeigniter controllers

Learn Codeigniter PHP Framework : Part 4 – Codeigniter controllers codeigniter routes requests to a default controller if the request doesn’t specify a controller’s name. Developers can set the default controller from the /system/application/config/routes. The configuration key is: $route[’default_controller’] = "welcome"; The default controller is a php class named welcome.php under controllers in the application […]
Continue reading…

Enjoyed this post? Share it!

 
 

What is loose coupling and component singularity in MVC

Loose Coupling: Coupling is the degree to which the components of a system rely on each other. The less the components depend on each other, the more re-usable and flexible the system becomes. A good MVC framework must be a very loosely coupled system. Component Singularity: Singularity is the degree to which components have a […]
Continue reading…

Enjoyed this post? Share it!

 
 

What is Model-View-Controller?

Trygve Reenskaug first devised MVC in the late 1970s for Smalltalk. The basic goal of MVC is separating user interface code into three separate areas. The three areas that MVC defines are Model, View, and Controller. These are responsible for domain logic, user interface, and control logic respectively. MVC has the following benefits: 1. Manipulating […]
Continue reading…

Enjoyed this post? Share it!