2011-07-01 46 views
2

我不能让我的指数()操作来传递一个有效的模式,我的评论()动作是什么(模型),并返回RedirectToAction返回查看的区别(“视图名”,模型)

.. 。的ActionResult指数()...

  else 
      { 
       return RedirectToAction("Review", wizard); <--wizard is a valid object here.... 

      } 

的ActionResult评论()

public ActionResult Review() 
    { 
     return View(_wizard); <-- THis is always null. 
    } 

更新: 这里是我的整个控制器。我想让用户从向导索引到评论页面,最后到达实际保存数据的传输页面。我的最后一块包裹着我的头脑,真的有问题。当你习惯了asp classic时,你必须从头开始明确地编写所有的东西,这很难适应MVC3中的Magic继承。所以,我敢打赌,我正在编写大量没有用的代码。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using mvc3test.Models; 
using Microsoft.Web.Mvc; 
using System.Web.Mvc; 
using mvc3test.Services; 

namespace mvc3test.Controllers 
{ 

    public class WizardController : Controller 
    { 

     private WizardViewModel wizard = new WizardViewModel(); 
     private DR405DBContext db; 

     public WizardController(IDBContext dbContext) 
     { 
      db = (DR405DBContext)dbContext; 
     } 

     public WizardController() 
     { 
      db = new DR405DBContext(); 
     } 

     public ActionResult Index() 
     { 

      wizard.Initialize(); 
      return View(wizard); 
     } 

     [HttpPost] 
     public ActionResult Index([Deserialize] WizardViewModel wizard, IStepViewModel step) 
     { 

      wizard.Steps[wizard.CurrentStepIndex] = step; 
      if (ModelState.IsValid) 
      { 
       if (!string.IsNullOrEmpty(Request["next"])) 
       { 
        wizard.CurrentStepIndex++; 
       } 
       else if (!string.IsNullOrEmpty(Request["prev"])) 
       { 
        wizard.CurrentStepIndex--; 
       } 
       else 
       { 
        return View("Review", wizard); 

       } 
      } 
      else if (!string.IsNullOrEmpty(Request["prev"])) 
      { 
       wizard.CurrentStepIndex--; 
      } 
      return View(wizard); 


     } 


     [AllowAnonymous] 
     public ActionResult Review(WizardViewModel model) 
     { 
      return View(model); 
     } 

     [AllowAnonymous] 
     [HttpGet] 
     public ActionResult Review(Int32 ID) 
     { 
      var service = new DR405Service(db); 
      var myWizard = service.WireUpDataModelToViewModel(service.DBContext.dr405s.Single(p => p.ID == ID)); 

      return View(myWizard); 
     } 


     public ActionResult Transmit() 
     { 
      var service = new DR405Service(db); 
      service.Wizard = wizard; 
      service.Save(); 
      return View(); 
     } 


    } 
} 
+0

您是否收到错误?该对象是否以其传入视图的方式存在,或者在它通过控制器之前是否为空?如果您提供的信息多于“This does not work”,那么它更容易帮助= D – BentOnCoding

+0

http://www.asp.net/mvc>学习资源>控制器基础 –

+0

@Chris如果我能够理解由微软你认为我会问这里吗? –

回答

2

msdnRedirectToAction将导致另一个获取请求的Review行动。

返回一个HTTP 302响应于 浏览器,这会导致浏览器 使GET请求到指定 动作。

这会导致wizard对象失去其价值并需要重新填充。

View()只是返回当前上下文中与该动作相关联的视图。

如果可能,您可以将向导放置在TempData,return View("Review", wizard)或将wizard作为路由值传递。

0

return RedirectToAction(“Review”,wizard);将向导对象传递给名为Review的视图。 Review需要是基于与向导相同的类的强类型视图。

如果这不能解答您的问题,发布您的视图代码会很有帮助。

1

RedirectToAction向浏览器返回HTTP 302响应,这会导致浏览器对指定操作发出GET请求。所以,你不能传递一个复杂的对象,你做

这是不是最好的解决方案,但尝试把精灵对象的ViewData重定向前:

ViewData["wizard"] = wizard 

,然后在审查得到它()

var wizard = (Wizard)ViewData["wizard"];