RedirectToAction(..) with complex deep object fails

前端 未结 4 1826
生来不讨喜
生来不讨喜 2021-01-04 13:18

I\'m trying to pass an object from one controller action to another. The object that I\'m passing around looks more or less like this:

public class Person
{
         


        
4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-04 14:16

    For everyone, that really needs to call some action and return view from another controller with complex object and don't want (or can't) to pass object in TempData. I use in my app very ugly but working solution:

    protected ActionResult InternalRedirectToAction(string action, string controller, object model)
    {
        var htmlHelper = new HtmlHelper(new ViewContext(
                                  ControllerContext,
                                  new WebFormView(ControllerContext, "HACK"),
                                  new ViewDataDictionary(),
                                  TempData, //for sharing TempData between Actions
                                  new StringWriter()),
                            new ViewPage());
    
        var otherViewHtml = htmlHelper.Action(action, controller, model);
        return Content(otherViewHtml.ToString());
    }
    

    Inspired by answer found here: https://stackoverflow.com/a/4360019/1341409

提交回复
热议问题