当前位置: 动力学知识库 > 问答 > 编程问答 >

asp.net mvc 3 - How to use the RedirectToAction correct

问题描述:

I have an problem using the RedirectToAction method in mvc. My Map route look like this.

 routes.MapRoute(

"Project", // Route name

"{Controller}/{Action}/{projectId}/{machineName}/{companyId}",

new { controller = "Project", action = "Directives", projectId = 0, machineName = "", companyId = 0 } // Parameter defaults);

And the return look like this:

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

It works prefect, but the i use the RedirectToAction it still looks like this

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

Controller:

 [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", "Project", new { projectId = 48, machineName = "Netduino", companyId = 27 });

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

}

else

{

ModelState.AddModelError("", "Invalid username or password");

}

return View();

}

The receiving controller:

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();

}

But the way i want it is like this.

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

But the weird thing is that i can manual type in the Url an it works prefect. What is im doing wrong?

网友答案:

I have a few tips.

First, don't name your routes. Pass null to that first argument. There are several RedirectToRoute overloads that take a route name, and RedirectToAction returns a RedirectToRouteResult.

Second, don't give defaults to your required parameters. If you want to have a default, give a default only to the last parameter.

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

Third, when in doubt, try returning a RedirectToRoute instead. Pass the controller, action, and area (if applicable) along with your other parameters:

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

Last tip is use T4MVC to get rid of those magic strings.

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

You can use those same strong types in RedirectToRoute, or you can use the T4MVC partials for returning to actions:

return RedirectToAction(MVC.Project.Directives
    (projectId, project.MachineName, user.Company_Id));
网友答案:

When I test your route and the RedirectToAction(), it works correctly when I correct the case of the "CompanyId" property in the anonymous object to "companyId".

return RedirectToAction("Directives", "Project", new { projectId = 48, machineName = "Netduino", companyId = 27 });

Update: Here is the code in global.asax, the Controllers, and the View.

routes.MapRoute(
             "Project",  // Route name
             "{Controller}/{Action}/{projectId}/{machineName}/{companyId}",
              new { controller = "Project", action = "Directives", projectId = 0, machineName = "", companyId = 0 }  // Parameter defaults
);

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return RedirectToAction("Directives", "Project", new { projectId = 48, machineName = "Netduino", companyId = 27 });
    }
}

public class ProjectController : Controller
{
    public ActionResult Directives(int projectId, string machineName, int companyId)
    {
        ViewBag.ProjectId = projectId;
        ViewBag.MachineName = machineName;
        ViewBag.CompanyId = companyId;
        return View();
    }
}

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Directives
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <h2>
        Directives</h2>
    <%: ViewBag.ProjectId %><br />
    <%: ViewBag.MachineName %><br />
    <%: ViewBag.CompanyId %><br />

    <%: this.Request.RawUrl %>
</asp:Content>
分享给朋友:
您可能感兴趣的文章:
随机阅读: