简体   繁体   English

将数据或参数从一个视图传递到另一个视图

[英]Passing data or parameters from one view to another

my current problem includes three classes let's say A, B and C. Class A includes one property of an object of class B and one of class C, a string in this case. 我当前的问题包括三个类,分别是A,B和C。类A包括类B的一个对象的属性和类C的一个属性(在这种情况下为字符串)。 the user should create a new object of type A in the first step without these two properties (only the other properties are asked). 用户应在第一步中创建一个没有这两个属性的类型为A的新对象(仅询问其他属性)。 An incomplete object of type A (let's call it a) should be built and in the next step the user shall decide whether he wants to match this object to an existing B or a new one. 应该构建一个不完整的类型为A的对象(我们称其为a),并在下一步中,用户应决定是要将此对象与现有的B对象还是新的对象进行匹配。 After this decision, the needed property is added to a and in the third step, the second property will be added in the same way and to finish it, the object a will be saved in the database. 做出此决定之后,将所需的属性添加到a中,然后在第三步中,以相同的方式添加第二个属性并完成该操作,将对象a保存在数据库中。

My current attempt looks like this: 我当前的尝试如下所示:

    public ActionResult Create()
    {
        return View();
    }

    //
    // POST: /A/Create

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(A a)
    {
        if (ModelState.IsValid)
        {
            return RedirectToAction("ViewForB", new { a = a});
        }
        return View(a);
    }
    public ActionResult ViewForB(A a)
    {
        return View();
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult ViewForB(B b, A a)
    {
        if (ModelState.IsValid)
        {
            bDb.Bs.Add(b);
            bDb.SaveChanges();
            a.propertyForB = b.title;
            return RedirectToAction("ViewForC", new {a = a});
        }
        return View(b);
    }

    public ActionResult ViewForC(A a)
    {
        return View();
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult ViewForC(C c, A a)
    {
        if (ModelState.IsValid)
        {
            cDb.Cs.Add(c);
            cDb.SaveChanges();
            a.propertyForC = c.title;
            aDb.As.Add(a);
            aDb.SaveChanges();
            return RedirectToAction("Index/");
        }
        return View();
    }

As you can see I tried to pass an A object to every next view but when submitting in ViewForB this won't work, because I don't know how to pass it in this case. 如您所见,我试图将A对象传递给每个下一个视图,但是在ViewForB中提交时将不起作用,因为在这种情况下我不知道如何传递它。 But in ViewForB objects of type B will also be built, therefore I decided to make it in this way to verify if the inputs for B are correct or not like in the create method. 但是在ViewForB中也会创建B类型的对象,因此我决定以这种方式进行验证,以验证B的输入是否正确(如create方法中一样)。

Do you know how to pass an object of type A to the last view, to complete and safe it there? 您知道如何将A类型的对象传递到最后一个视图,以在那里完成和保护它吗?

Thank you very much, any answer would be helpful. 非常感谢您,任何答案都将有所帮助。

There's no simple way to "pass" an arbitrary object from one view to another like this. 这样没有简单的方法将任意对象从一个视图“传递”到另一个视图。 When you return a RedirectResult you are returning an instruction to the browser to send a fresh request to the next URL - you can include simple query string parameters in this but there is no convention for serializing an arbitrary object to a set of query string parameters. 当您返回RedirectResult您正在向浏览器返回一条指令,以向下一个URL发送新请求-您可以在其中包含简单的查询字符串参数,但是没有约定将任意对象序列化为一组查询字符串参数。

You have various options: 您有多种选择:

  1. Store the information in a cookie and retrieve it in each action. 将信息存储在cookie中,并在每个操作中检索它。
  2. Return views directly from your POST actions, include hidden fields for fields that have already been populated and gradually build up a complete model. 直接从POST操作返回视图,包括已填充字段的隐藏字段,并逐步建立完整的模型。
  3. Use Session or some other server-side storage mechanism (eg a secondary database) to save progress. 使用会话或其他服务器端存储机制(例如,辅助数据库)保存进度。

You can also pass individual properties in the redirect instead of an entire model (you can still bind back to a model in the next action). 您还可以在重定向中传递单个属性,而不是传递整个模型(您仍可以在下一个操作中绑定回模型)。 This could be used either to pass keys for a model stored in a cookie or in Session or - if you only have a few properties that you need - to just pass them all individually: 这可以用于传递存储在cookie或Session中的模型的密钥,或者-如果您只需要一些属性,则可以单独传递它们:

return RedirectToAction("ViewForC",
    new { Something = A.SomeProperty, SomethingElse = A.AnotherProperty });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM