Click Here to Download SetDefaultActionInMVC Example.
You can set default action name for controller in MVC. By providing default action name you did not type action name in controller. It will automatically call default action.
We can configure Route in 'App_Start/RouteConfig.cs' file using this 'routes.MapRoute' method, where 'routes' is the object of 'RouteCollection' class. In 'MapRoute' method we can set Default values. Code is something like below :
Here is example for this.
In this example if you want customer list you need to call this URL 'Customer/List'. But if you want to set "List" action as Default action for "Customer" controller you can do this by setting "List" as default action for that controller using "MapRoute" method.
Route Config (RouteConfig.cs) :
Model (Customer.cs):
Customer Controller (CustomerController.cs) :
List View(List.chtml) :
Output :
Below are the books that you would like :
You can set default action name for controller in MVC. By providing default action name you did not type action name in controller. It will automatically call default action.
We can configure Route in 'App_Start/RouteConfig.cs' file using this 'routes.MapRoute' method, where 'routes' is the object of 'RouteCollection' class. In 'MapRoute' method we can set Default values. Code is something like below :
routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { id = UrlParameter.Optional,Action="List" } );
Here is example for this.
In this example if you want customer list you need to call this URL 'Customer/List'. But if you want to set "List" action as Default action for "Customer" controller you can do this by setting "List" as default action for that controller using "MapRoute" method.
Route Config (RouteConfig.cs) :
public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { id = UrlParameter.Optional,Action="List" } ); } }
Model (Customer.cs):
public class Customers { public int CustomerId; public string FirstName; public string LastName; public int Rank; public static List<Customers> GetCustomersList() { List<Customers> lstCustomers= new List<Customers>(); Customers objCustomers; objCustomers = new Customers(); objCustomers.CustomerId=1; objCustomers.FirstName="Sachin"; objCustomers.LastName="Tendulkar"; objCustomers.Rank=1; lstCustomers.Add(objCustomers); objCustomers = new Customers(); objCustomers.CustomerId = 2; objCustomers.FirstName = "Lionel"; objCustomers.LastName = "Messi"; objCustomers.Rank = 5; lstCustomers.Add(objCustomers); objCustomers = new Customers(); objCustomers.CustomerId = 3; objCustomers.FirstName = "Cristiano "; objCustomers.LastName = "Ronaldo"; objCustomers.Rank = 6; lstCustomers.Add(objCustomers); objCustomers = new Customers(); objCustomers.CustomerId = 4; objCustomers.FirstName = "Rahul"; objCustomers.LastName = "Dravid"; objCustomers.Rank = 2; lstCustomers.Add(objCustomers); objCustomers = new Customers(); objCustomers.CustomerId = 5; objCustomers.FirstName = "Brad"; objCustomers.LastName = "Pitt"; objCustomers.Rank = 3; lstCustomers.Add(objCustomers); return lstCustomers; } }
Customer Controller (CustomerController.cs) :
public class CustomerController : Controller { // // GET: /Customer/ public ActionResult Index() { return View(); } public ActionResult List() { List<Customers> lstCustomers = Customers.GetCustomersList(); return View(lstCustomers); } public ActionResult Edit(int id) { List<Customers> lstCustomers = Customers.GetCustomersList(); Customers objCustomer=(from m in lstCustomers where m.CustomerId==id select m).SingleOrDefault(); return View(objCustomer); } }
List View(List.chtml) :
@model IEnumerable<SetDefaultRoute.Models.Customers> @{ ViewBag.Title = "List"; } <h2>List</h2> <p> @Html.ActionLink("Create New", "Create") </p> <table> <tr> <th>Customer Id</th> <th>First Name</th> <th>Last Name</th> <th>Rank</th> </tr> @foreach (var item in Model) { <tr> <td>@Html.DisplayFor(model => item.CustomerId) </td> <td>@Html.DisplayFor(model => item.FirstName)</td> <td>@Html.DisplayFor(model => item.LastName)</td> <td>@Html.DisplayFor(model => item.Rank)</td> <td> @Html.ActionLink("Edit", "Edit", new { id = item.CustomerId }) | @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) | @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ }) </td> </tr> } </table>
Output :
Below are the books that you would like :
No comments:
Post a Comment