建设一步一个复杂的对象一步。在哪里保存呢? [英] Building a complex Object step by step. Where to save it?

查看:122
本文介绍了建设一步一个复杂的对象一步。在哪里保存呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ASP.NET MVC。我的要求是建立通过一步一步的过程复杂对象(由其他对象的对象),就像一个向导。

I am using ASP.NET MVC. My requirement is to build a complex object (an object made of other object) through a step-by-step procedure like in a wizard.

每一个依赖对象应建立在这一步,应确认在它的一步。例如:

Every dependent object shall be build on it's step and shall be validated in it's step. For example

public class ComplexObjectModel {
    public Object1 MyObject1 { get; set; }
    public Object2 MyObject1 { get; set; }
    public Object3 MyObject1 { get; set; }
}

由于没有一个精灵我决定创建绑定到这些模型3模型类和3强类型的局部视图没有内置的工具。

As there is no built-in facility for a wizard I have decided to create 3 model classes and 3 strong typed partial views binded to these models.

在我的伪向导的每一步我验证了相关模型对象和复杂对象的属性设置为它的参考。

On every step of my pseudo wizard I validate the dependent model object and set the property of the complex object to its reference.

我想保存的ViewData / TempData的内部复杂的对象以下列方式

I was thinking to save the complex object inside the ViewData/TempData in the following way

在控制器动作

[HttpPost]
public ActionResult MyAction1() {
    ComplexObjectModel com = (ComplexObjectModel)ViewData["ComplexObjectModel"];
    com.MyObject1 = new Object1();
    ViewData["ComplexObjectModel"] = com;
    return PartialView( "MyAction2", com.Object1 );
}

和在视图

<% using (Html.BeginForm()) { %>
    <%= Html.Hidden("ComplexObjectModel", ViewData["ComplexObjectModel"]) %>

    ... view fields for Object1, Object n ....
<% } %>

但这样做这样的对象不通过视图和控制器,我已经经历了为空时,它从视图中的下一个动作回来背之间的往复。

But doing this way the object is not passed back-and-forth between the view and the controller and I have experienced that is null when it comes back from the view to the next action.

有没有支持这一需求的一种方式?

Is there a way to support this requirement?

感谢帮助

推荐答案

有几个方面我可能会解决这个。

There are a couple of ways I might tackle this.

第一;我可能会决定来存储所有这一切在会话对象。我每次我去到下一个页面这里假设模型是相当大的,所以我不希望他们存储在视图并传回。

First; I might decide to store all this in the session object. I am assuming here that the models are quite large and so I wouldn't want them stored on the view and passed back each time I go to the next page.

二;我可以将它们存储在数据库中,如果向导没有完成,然后删除它们作为后台进程的一部分。

Second; I might store them in the database and if the wizard didn't complete then delete them as part of a background process.

有一件事我是不会做的就是通过复杂的对象每个视图。视图应该真正需要知道的关于在一个宁静的世界上任何其他视图什么,所以我不倾向于这样做。

The one thing I wouldn't do is pass the complex object to each view. The view should really need to know anything about any other view in a restful world and so I'd be inclined not to do it.

当然,这并不意味着你需要决定的存储位置的数据。如果我有一个大objcect话,我会选择数据库,如果是相当小的话,我会选择会话对象。

Of course that does mean you need to decide a storage place for the data. If I had a Large objcect then I'd choose the database and if was fairly small then I'd choose the session object.

正如我们已经发现,具有用于在每个视图中的每个对象中的所有数据可能会产生问题。

As you have already found, having all the data for each object in each view can be problematic.

不过,如果你有决心做到这一点的视图那么这里的方法是什么,我会做;

However, if you are determined to do this the View way then here is what I'd do;


  1. 创建哪些交易的局部视图
    只用在复杂的每个对象
    模型。

  2. 在每个视图,包括所有的三个或
    以上,部分意见。

  3. 对于这不是每个部分的视图
    的积极参与者的看法,
    将它放在一个div是内
    隐藏。

至少这样,当您更改属性,或添加一个,你只需设置在局部视图一次,而不是三次。此外,如果有错误,可以取消隐藏的div,看看数据是否在未来。

At least then when you change a property, or add one, you simply set it in the partial view once and not three times. Also if there is an error, you can unhide the divs and see if the data is coming in.

另外每个字段然后应ModelName.Property的id使得控制器知道其中属性是

Also each field should then have the id of ModelName.Property so that the controller knows where the property is.

<%= Html.TextBox("MyObject1.MyProperty1", value) %>

然后你只需做控制器,这种即兴;

Then in the controller you simply do, and this off the cuff;

[HttpPost]
public ActionResult MyAction1(ComplexObjectModel complexModel) {

这篇关于建设一步一个复杂的对象一步。在哪里保存呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆