MVC控制器动作参数为null [英] MVC Controller Action Parameter is null

查看:130
本文介绍了MVC控制器动作参数为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有控制器名称:区和动作名称:Incharges但我想要的网址是这样(有一些paremeter动作名称)

www.example.com/district/incharges/aaa

www.example.com/district/incharges/bbb

www.example.com/district/incharges/ccc

不过,在调试teamName总是在行动参数返回为NULL。

路由

  routes.MapRoute(
                名称:默认,
                网址:{控制器} / {行动} / {ID}
                默认:新{控制器=家,行动=索引,ID = UrlParameter.Optional}
            );            routes.MapRoute(
            DistrictDetails
            区/ Incharges / {} teamName
            新{控制器=区,行动=Incharges}
            );

控制器

不过,在调试teamName总是在行动参数返回为NULL。

 公共类DistrictController:控制器
    {
        公众的ActionResult Incharges(字符串teamName)
        {
            Incharge presentationVM INPVM =新Incharge presentationVM();
            INPVM.InitializePath(teamName,的String.Empty);
            返回视图(,INPVM);
        }
}

查看

  @ {
    ViewBag.Title =指数;
}< H2>指数关于< / H>


解决方案

具体路线,你必须声明的第一个

  routes.MapRoute(
            DistrictDetails
            区/ Incharges / {} teamName
            新{控制器=区,行动=Incharges,ID = UrlParameter.Optional}routes.MapRoute(
                名称:默认,
                网址:{控制器} / {行动} / {ID}
                默认:新{控制器=家,行动=索引,ID = UrlParameter.Optional}
            ));

I have Controller name: District and Action name: Incharges But I want the URL to be like this (action name with some paremeter)

www.example.com/district/incharges/aaa

www.example.com/district/incharges/bbb

www.example.com/district/incharges/ccc

But, while debugging teamName always return as NULL in the action parameter.

Routing

routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );

            routes.MapRoute(
            "DistrictDetails",
            "District/Incharges/{teamName}",
            new { controller = "District", action = "Incharges" }
            ); 

Controller

But, while debugging teamName always return as NULL in the action parameter.

public class DistrictController : Controller
    {     


        public ActionResult Incharges(string teamName)
        {
            InchargePresentationVM INPVM = new InchargePresentationVM();
            INPVM.InitializePath(teamName, string.Empty);
            return View("", INPVM);
        }
}

View

@{
    ViewBag.Title = "Index";
}

<h2>Index About</h2>

解决方案

specific route you have to declare the first

routes.MapRoute(
            "DistrictDetails",
            "District/Incharges/{teamName}",
            new { controller = "District", action = "Incharges", id = UrlParameter.Optional }

routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );); 

这篇关于MVC控制器动作参数为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆