具有复杂类型参数的RedirectToAction

时间:2018-08-13 17:52:14

标签: c# asp.net-mvc redirect parameters parameter-passing

我不知道如何通过重定向发送DTO。

我已经尝试了在this Questionthis documentation中都可以找到的一切。

我该怎么做?

下面是我尝试过的内容和结果的记录:

var code  = HttpUtility.UrlEncode(await UserManager.GenerateEmailConfirmationTokenAsync(userId))

发送空的dto:

public ActionResult Create(ComRuleCompoundKeyDto ruleKey)
{
    var dto = new ComRuleOverrideDto()
    {
        Company = 42
    };

发送StupidDto填充为空:

    //var routeValues = new RouteValueDictionary
    //{
    //    { "ruleKey", ruleKey },
    //    { "dto", dto },
    //};
    //return RedirectToAction(nameof(MaintainOverride), routeValues);

发送填充有默认值的dto:

    //var routeValues = new RouteValueDictionary(new StupidDto
    //{
    //    ruleKey = ruleKey,
    //    dto = dto
    //});
    //return RedirectToAction(nameof(MaintainOverride), routeValues);

发送空的dto:

    //ControllerContext.RouteData.Values.Add("ruleKey", ruleKey);
    //ControllerContext.RouteData.Values.Add("dto", dto);
    //return RedirectToAction(nameof(MaintainOverride));

System.InvalidOperationException:找不到视图“创建”或其主视图,或者没有视图引擎支持搜索到的位置:

    //return RedirectToAction(nameof(MaintainOverride), new { ruleKey, dto });

1 个答案:

答案 0 :(得分:0)

我设法通过在返回另一个操作(与重定向相对)时显式设置视图的名称(nameof(MaintainOverride))来使其工作。

public ActionResult Create(ComRuleCompoundKeyDto ruleKey)
{
    var dto = new ComRuleOverrideDto()
    {
        Company = 42
    };

    return MaintainOverride(ruleKey, dto);
}

public ActionResult MaintainOverride(ComRuleCompoundKeyDto ruleKey, ComRuleOverrideDto dto)
{
    var currencies = _srv.GetAllCurrenciesUsedByDealers().Select(x => x.CurrencyCode);

    var custTypes = _srv.GetAllCustomerTypes();
    var unitIds = _srv.GetUnitIds();
    var vm = new OverridesViewModel(currencies, custTypes, unitIds, ruleKey, dto);

    return View(nameof(MaintainOverride), vm);
}