加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc-3 – 如何正确使用RedirectToAction

发布时间:2020-12-16 04:09:12 所属栏目:asp.Net 来源:网络整理
导读:我在mvc中使用RedirectToAction方法时遇到问题.我的地图路线看起来像这样. routes.MapRoute( "Project",// Route name "{Controller}/{Action}/{projectId}/{machineName}/{companyId}",new { controller = "Project",action = "Directives",projectId = 0,m
我在mvc中使用RedirectToAction方法时遇到问题.我的地图路线看起来像这样.
routes.MapRoute(
         "Project",// Route name
         "{Controller}/{Action}/{projectId}/{machineName}/{companyId}",new { controller = "Project",action = "Directives",projectId = 0,machineName = "",companyId = 0 }  // Parameter defaults);

回报看起来像这样:

return RedirectToAction("Directives","Project",new { projectId = projectId,machineName = project.MachineName,CompanyId = user.Company_Id });

它工作正常,但我使用RedirectToAction它仍然看起来像这样

http://localhost:1843/Project/Directives?projectId=48&machineName=Netduino&companyId=27

控制器:

[HttpPost]
    [ValidateInput(false)]
    public ActionResult Create(Project project,string text)
    {
        ViewBag.MachineType = new List<string>(new string[] { "Machine Type A","Machine Type B","Machine Type C","Machine Type D","Machine Type E" });

        if (ModelState.IsValid)
        {
            UserAccess user = (UserAccess)(Session["UserAccessInfo"]);

            Int64 projectId = DataBase.DBProject.InsertProject(project.ProjectNumber,project.MachineName,project.MachineNameEnglish,1,project.Serial,text,user.Company_Id,project.MachineType);

            return RedirectToAction ("Directives",new { projectId = 48,machineName = "Netduino",companyId = 27 });
           // return RedirectToAction("Directives",CompanyId = user.Company_Id });
        }
        else
        {
            ModelState.AddModelError("","Invalid username or password");
        }

        return View();

    }

接收控制器:

public ActionResult Directives(int projectId,string machineName,int companyId)
    {
        convertedDirectives = new List<Models.Directives>();
        ViewBag.MachineName = machineName;
        ViewBag.ProjectId = projectId;
        List<object> Directives = new List<object>();

        if (ModelState.IsValid)
        {
            Directives = DataBase.DBProject.GetDirectives(companyId);
            foreach (List<object> dir in Directives)
            {
                Directives newDirective = new Models.Directives();
                newDirective.CompanyID = Convert.ToInt32(dir[0]);
                newDirective.DirectiveId = Convert.ToInt32(dir[1]);
                newDirective.Id = Convert.ToInt32(dir[2]);
                newDirective.DirectiveName = Convert.ToString(dir[3]);
                newDirective.DirectiveNameShort = Convert.ToString(dir[4]);
                newDirective.Created = Convert.ToDateTime(dir[5]);

                convertedDirectives.Add(newDirective);
            }
        }
        else
        {
            ModelState.AddModelError("","An error has ");
        }

        ViewBag.DirectivesList = convertedDirectives;
        return View();
    }

但我想要的方式是这样的.

http://localhost:1843/Project/Directives/48/Netduino/27

但奇怪的是,我可以手动输入Url并且它可以完美运行.我做错了什么?

解决方法

我有一些提示.

首先,不要命名您的路线.将null传递给第一个参数.有几个RedirectToRoute重载采用路由名称,RedirectToAction返回RedirectToRouteResult.

其次,不要给出所需参数的默认值.如果要使用默认值,请仅将默认值设置为最后一个参数.

routes.MapRoute(null,// don't name the route
    "{controller}/{action}/{projectId}/{machineName}/{companyId}",new
    {
        controller = "Project",//projectId = 0,//machineName = "",//companyId = 0,}
);

第三,如有疑问,请尝试返回RedirectToRoute.传递控制器,动作和区域(如果适用)以及其他参数:

return RedirectToRoute(new
{
    controller = "Project",// area = "SomeAreaName",// if the action is in an area
    projectId = projectId,companyId = user.Company_Id,});

最后一个提示是使用T4MVC摆脱那些神奇的字符串.

routes.MapRoute(null,new
    {
        controller = MVC.Project.Name,action = MVC.Project.ActionNames.Directives,}
);

您可以在RedirectToRoute中使用相同的强类型,也可以使用T4MVC部分返回操作:

return RedirectToAction(MVC.Project.Directives
    (projectId,user.Company_Id));

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读