MVC - Passing Data with RedirectToAction()

前端 未结 8 2084
既然无缘
既然无缘 2020-12-01 15:17

I\'d like to take data entered in an MVC user form and display it in a different view.

The class has the following private variable:

IList

        
相关标签:
8条回答
  • 2020-12-01 15:58

    It sounds like you're trying to do:

    public ActionResult UpdateContent(FormCollection form) {
        ...
        return View("Preview", _pagecontent);
    }
    

    Note that a redirection is supposed to be a "clean slate" for the browser (except for things like the auth cookie). You don't get to tell the browser to pass information along to the next request, since the next request should be able to stand on its own. All you get to do is tell the browser what URL to request next. In ASP.NET MVC, when you pass an arguments-object to RedirectToAction, the public properties of that object are appended as query-string parameters to the generated URL.

    0 讨论(0)
  • 2020-12-01 15:59

    This is not working because RedirectToAction is actually sending back a Http 302 to the browser. When the browser receives this 302, it does a new request to the server asking for the new page. New request, new temp variables.

    You will also face this problem when you try to save/edit/delete something and for some reason you deny it and you have to return the old form again.

    So, instead of:

    return RedirectToAction("Preview", _pagecontent);
    

    Put the Preview logic in a separate method and just call it:

    return PreviewLogic(_pagecontent);
    

    You can also use the TempData[] dic to persist data for the next request like others have said, but then you will not avoid the 302 additional round trip to the server.

    0 讨论(0)
  • 2020-12-01 16:01

    Can't you just make 2 action results with the same name and mark 1 of them with HttpPost?

        public ActionResult UpdateContent(FormCollection preview = null)
        {
            return View(preview);
        }
        [HttpPost]
        public ActionResult UpdateContent(FormCollection collection = null, bool preview = false)
        {
            if (preview)
                return UpdateContent(collection);
            else
                return UpdateContent(null);
        }
    
    0 讨论(0)
  • 2020-12-01 16:09

    Be careful when using TempData. It works great in a single server environment but in a cloud environment it may not work as expected since you cannot guarantee that the request will hit the same machine. This happens because TempData rely on the asp.net session. But if you are using other session manager like SQL or AppFabric Cache it will work fine.

    0 讨论(0)
  • 2020-12-01 16:13

    It looks like you are looking for the UpdateModel command:

    Check out ScottGu's blog post on the topic:

    Improved UpdateModel and TryUpdateModel methods

    0 讨论(0)
  • 2020-12-01 16:14

    The second parameter to RedirectAction is routeValues, not model.

    protected internal RedirectToRouteResult RedirectToAction(string actionName, object routeValues);
    

    Try using TempData for the model. Its for persisting data between redirects.

    0 讨论(0)
提交回复
热议问题