Click here to Skip to main content
15,900,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please explain.

When i create a new view in MVC by default i am getting Index page. I want to display the name of view according to me. When i rename Index view its through error. Please Explain the same
Posted

1 solution

Naming your actions is up to you. But you have to set up routing accordingly.
Read this article: http://www.asp.net/mvc/tutorials/older-versions/controllers-and-routing/asp-net-mvc-routing-overview-cs[^]
As you will see, there is a default route mapping:
C#
routes.MapRoute(
                "Default",                                              // Route name
                "{controller}/{action}/{id}",                           // URL with parameters
                new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
            );

So if you don't specify an action name it will use Index. This is what you need to change in addition to renaming the action method. And of course, you need also to have proper views prepared (*.cshtml).

So let's say you want startpage instead of index. You need to do followings:
1) change the default route from index to startpage
2) rename index method to startpage in the controller
3) rename index.cshtml to startpage.cshtml

If you start with bare application, you need only step 1, and proceed with the actions.
 
Share this answer
 
Comments
rajnish.d3006 24-Jun-13 2:46am    
Actually i want to say that.

1.) This my controller

public ActionResut Index()
{
return View();
}

I want to rename Index View.
Like

2.) public ActionResult Order()
{
return View()
}

this view shows error. Please explain
Zoltán Zörgő 24-Jun-13 2:57am    
Ok, but do you have an order.cshtml also? That is what is processed and returned. But it won't become default. If you want to make it default in that controller only, you will need to add a new route instead of changing the default route.
rajnish.d3006 24-Jun-13 3:57am    
Dear Zoltán Zörgő,

Actually i am a C# Asp.net developer. I am trying to learn MVC. I have lots of Question.

Please give some suggestion to learn MVC 4. Or provide simple or small project to learn same. Thank you very much for your reply.
Zoltán Zörgő 24-Jun-13 4:06am    
Nothing is better than the official tutorial. Start here:
http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/intro-to-aspnet-mvc-4
If you want a book, I suggest this one: http://www.manning.com/palermo3/
rajnish.d3006 24-Jun-13 4:12am    
Thanks alot

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900