开发者

MVC and routing with strong typed views?

Hi,

Say that I got a action that returns to a strong typed view. This type is also the inparameter to the a开发者_如何学编程ction. To set the current page number(for a list on the page) I need to set Model.ALS.P (our strong type).

When sending a requestion to the servier the quarystring myComputer.com/MyController/MyAction?ALS.P=2 will set the ALS.P to 2 on the incoming object to the action.

If I want to convert this to the following URL : myComputer.com/MyController/MyAction/2 how would the route look like?

I have tried this :

routes.MapRoute(
                "List", // Route name
                "{controller}/{action}/{ALS_P}", // URL with parameters
                new { controller = "Ad", action = "List", ALS_P = 0 } // Parameter defaults
            );

But that do not work and it will not accept ALS.P as parameter name.

BestRegards


You know the route you're defining is the same as the default route in asp.net mvc? That means, if you didn't remove the default route definition, yours will never get hit.

You should leave the default route, and create a custom modelbinder to bind the id routeparameter to the appropriate property of your model. See this question for more detail.

Otherwise, there's also the routedebugger (available from nuget) which can show you more insight into your routes and how (when) they are hit.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜