【问题标题】:Redirect to Action with complex model as parameter that has an array of string property重定向到具有复杂模型的动作作为具有字符串属性数组的参数
【发布时间】:2013-01-11 07:13:36
【问题描述】:

我遇到了一个问题,我有谷歌但我没有找到解决方法。

我有这两个Action

    public ActionResult ActionA(DTOA dtoA)
    {
       .....
    }

    [HttpPost]
    public ActionResult ActionB(DTOB dtoB)
    {
        DTOA dto = new DTOA();
        dto.ArraOfStringA = dtoB.ArraOfStringB;
        dto.Id = dtoB.Id;


        return RedirectToAction("ActionA", dto);
    }

模型

public class DTOA
{
    public int Id{get;set;}
    public string[] ArraOfStringA { get; set; }

}

public class DTOB
{
    public int Id{get;set;}
    public string[] ArraOfStringB { get; set; }

}

情况是这样的

当我发布到 ActionB 时,dtoB 参数的字符串数组填充了 2 个元素。 “1”和“2”

但是当这个动作重定向到ActionA时,dtoA参数填充了1个itens。 "System.String[]"。

如果我在浏览器中输入“domain/controler/ActionA?ArraOfStringA=1&ArraOfStringA=2”

dtoA 参数由两个元素填充。 “1”和“2”(预期行为)

那么,我怎样才能重定向到 ActionA 传递具有数组 os 字符串属性的复杂模型?

【问题讨论】:

    标签: c# asp.net-mvc asp.net-mvc-3


    【解决方案1】:

    如果模型很复杂,您可能需要考虑在两个动作之间传输时使用TempData 来存储模型,而不是使用路由参数。

    public ActionResult ActionA()
    {
       var dto = TempData["model"] as DTOA;
    
       ...
    }
    
    [HttpPost]
    public ActionResult ActionB(DTOB dtoB)
    {
        DTOA dto = new DTOA();
        dto.ArraOfStringA = dtoB.ArraOfStringB;
        dto.Id = dtoB.Id;
    
        TempData["model"] = dto;
    
        return RedirectToAction("ActionA");
    }
    

    【讨论】:

    • 真的吗?你认为这是一个不好的方法 Make and Redirect("domain/controller/ActionA?ArrayOfStringA=1&ArrayOfStringA=2")
    • 一方面我同意继续使用 tempdata 的精神,另一方面我质疑为什么这应该如此困难。我了解 View 和 Controller 之间的映射模型有多困难,但在这种情况下,它应该像调用签名方法一样简单。
    • @DaveA - 我关心的是作为模型的任意长度字符串数组的 url 长度。可能有许多其他方法可以做到这一点;甚至可能不需要进行重定向,但没有更多很难说的信息。
    猜你喜欢
    • 1970-01-01
    • 2015-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-14
    • 1970-01-01
    相关资源
    最近更新 更多