2012-05-11 15 views
7

मुझे mvc में RedirectToAction विधि का उपयोग करने में कोई समस्या है। मेरा नक्शा मार्ग इस तरह दिखता है।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", "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(); 

    } 

प्राप्त करना नियंत्रक:

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 

लेकिन अजीब बात यह है कि मैं कर सकता हूँ यूआरएल में मैनुअल प्रकार एक यह प्रीफेक्ट काम करता है। मैं गलत क्या कर रहा हूँ?

उत्तर

4

मैं कुछ सुझाव दिए गए है।

सबसे पहले, अपने मार्गों का नाम न दें। उस पहले तर्क के लिए शून्य पास करें। कई RedirectToRoute ओवरलोड हैं जो रूट नाम लेते हैं, और रीडायरेक्ट टॉएक्शन एक RedirectToRouteResult देता है।

दूसरा, अपने आवश्यक पैरामीटर को डिफ़ॉल्ट न दें। यदि आप डिफ़ॉल्ट होना चाहते हैं, तो केवल अंतिम पैरामीटर पर डिफ़ॉल्ट दें।

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

तीसरा, संदेह में, इसके बजाय एक RedirectToRoute लौटने का प्रयास करें। दर्रा नियंत्रक, कार्रवाई, और क्षेत्र (यदि लागू हो) अपने अन्य पैरामीटर के साथ:

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, 
}); 

अंतिम टिप T4MVC का उपयोग उन जादू तार से छुटकारा पाने के लिए है।

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

आप RedirectToRoute में उन्हीं मजबूत प्रकार का उपयोग कर सकते हैं, या आप कार्यों की ओर लौटने के लिए T4MVC आंशिक उपयोग कर सकते हैं:

return RedirectToAction(MVC.Project.Directives 
    (projectId, project.MachineName, user.Company_Id)); 
+0

हैलो। किसी कारण से मैं अभी भी इसे सही तरीके से काम नहीं कर सकता। मैंने अपना कंट्रोलर जोड़ा है जो डेटा – mortenstarck

+1

भेजता है, मुझे अभी इसे काम करने के लिए मिला है। समस्या कई जगह थी। 1. Global.asax.cs फ़ाइल में मुझे इस परियोजना "/ {action}/{machineName}/{projectId}/{directiveId}/{directiveName}" जैसी विशिष्टता की आवश्यकता है। और मैंने ToAction के बजाय RedirectToRoute का उपयोग करने के आपके सुझाव का उपयोग किया और फिर मैंने काम किया। – mortenstarck

0

जब मैं आपके मार्ग और RedirectToAction() का परीक्षण करता हूं, तो यह सही ढंग से काम करता है जब मैं अज्ञात ऑब्जेक्ट में "companyId" संपत्ति के मामले को "companyId" में सही करता हूं।

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

अद्यतन: यहाँ Global.asax में कोड, नियंत्रकों, और दृश्य है।

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> 
+0

im गलत क्या कर रहा है। मैं अभी भी इसे सही नहीं कर सकता। लेकिन अगर मैं सही में यूआरएल टाइप करता हूं तो यह भी काम करता है – mortenstarck

संबंधित मुद्दे