views:

354

answers:

2

Hi All, I'm begginer in asp.net mvc and I have some doutbs about routes.

Im' developing a system to manage documents and I need make an URL like this:

        routes.MapRoute("Documentos",
            "{controller}/{documentType}/{documento}/{action}/{id}",
            new
            {
                controller = "Home",
                documentType = "",
                documento = "",
                action = "Index",
                id = UrlParameter.Optional
            });

and the app working an URL like theses:

"Document/Administrative/Contract" - (Index action by default to list documents of type 'Contract') "Document/Administrative/Contract/New" - (new action in controller) "Document/Administrative/Contract/10" - (detail action in controller) "Document/Administrative/Contract/Edit/10" - (edit action in controller)

Document would be a Controller, and Administrative would be just a description in url to identify that documents of 'Contract' is Administrative...

So, My doubts is about my controllers and actions, How should be the signature of the methods of controller ? Need I make an Area called Documents to do this more easy ?

PS: Sorry for my english!

Thanks a lot, Cheers!

Felipe

A: 

Just have 1 controller Document.

In this controller have actions Index , New and Edit.

All actions need to have parameters documentType and documento or (better) you have a Viewmodel as a parameter that has properties documentType and documento.

Pass this Viewmodel to the view. In the view use Html.ActionLink to generate Links that have documentType and documento set.

Malcolm Frexner
A: 
 routes.MapRoute("Documentos", 
            "Document/{documentType}/{documento}/{action}/{id}", 
            new 
            { 
                controller = "Document", 
                documentType = "", 
                documento = "", 
                action = "Index", 
                id = UrlParameter.Optional 
            }); 

When you want an URL of "Document/Administrative/Contract" to default to the List you have to make a hardcoded route like this:

 routes.MapRoute("Documentos", 
            "Document/Administrative/Contract", 
            new 
            { 
                controller = "Document", 
                documentType = "Administrative", 
                documento = "Contract", 
                action = "List"
            }); 

Your actions should plainly contain the same names for the parameters as you use in routes. Or combine the parameters in Model classes.

public ActionResult(string documentType, string documento, int id)

or

public ActionResult(Document doc)

if you use Model class Document.

public class Document
string documentType;
string documento;
int id;

HTH

mare