开发者

What is the difference between return View(model) and return RedirectToAction("ViewName",model)

开发者 https://www.devze.com 2023-03-16 22:44 出处:网络
I can\'t get my Index() action to Pass a valid model to my Review()Action ... ActionResult Index()... else

I can't get my Index() action to Pass a valid model to my Review() Action

... ActionResult Index()...

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

            }

ActionResult Review()

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

Update: Here is my whole controller. I want to take the user from the wizard index, to a review page, then finally to a transmit page that actually saves the data. I'm having real problems wrapping my head around the final piece. When you are used to asp classic where you have to explicitly write everything from scratch, it's kind of hard to get used to the Magic inherit in MVC3. So, I bet I'm writing a lot of uneeded code.

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(WizardV开发者_如何学JAVAiewModel 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();
        }


    }
}


Per msdn RedirectToAction will cause another get request to the Review action.

Returns an HTTP 302 response to the browser, which causes the browser to make a GET request to the specified action.

This causes the wizard object to loose its value and needs to be repopulated.

View() simply returns the view associated with that action within the current context.

You could either place wizard in TempData, return View("Review", wizard), or have wizard passed as route values if possible.


RedirectToAction returns an HTTP 302 response to the browser, which causes the browser to make a GET request to the specified action. So you can't pass a complex object as you do

It's not the best solution but try to put wizard object before redirecting in ViewData :

ViewData["wizard"] = wizard

and then get it in Review()

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


return RedirectToAction("Review", wizard); passed the wizard object to the view named Review. Review will need to be a strongly typed view based on the same class as wizard.

Posting your view code would be helpful if this does not answer your question.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号