English 中文(简体)
列出用于创建和编辑的属性和控制器操作
原标题:List Properties and Controller Actions for Creation and Editting

首先,请告诉我问题不清楚的地方,因为我不习惯在网上提问

Problem in a Nutshell:
The framework makes it very easy to create editors for a viewmodel with simple scalar properties. However it does not scale well when we want to edit viewmodels that have lists of other objects. The framework should not assume the availability of a session neither javascript.

Code Sample
I posted all the meaningful code here, which should give much better context than describing the problem with words. I kept it as simple as possible:

namespace Simple{
    class SimpleController{
        public Html CreateA(){
            return View(new A());
        }

        // this is usually an http post responding to a button under the displayed listing of Bs in the main form
        [HttpPost]
        public Html CreateA(A a, FormCollection collection){
            // ***  This is only necessary in the case of lists ***
            // ***  what happens in the "MoreInvolved" case is
            //     terrifying. I didn t post any attempt. ***
            if(collection["button"] == "AddB"){
                a.listB.Add(new B());
                return View(a);
            }
            else{
                // save a to database
                // return redirect either to list or to details of created object
            }
        }
    }

    class A {
        public string a1;
        public string a2;
        public string a3;
        public List<B> listB;
    }

    class B {
        public string b1;
        public string b2;
    }
}

namespace MoreInvolved {
    class A {
        public string a1;
        public string a2;
        public string a3;
        public List<B> listB;
    }

    class B {
        public string b1;
        public string b2;
        public List<C> listC;
    }

    class C {
        public string c1;
        public string c2;
    }
}

对于这个简单的案例,代码中没有包含这些视图,但它已经过测试,运行良好。我使用了表达式树来简化它,但这并没有发布在代码中。然而,在更为复杂的案件中,我还没有完成,除了思考离奇和奇怪之外,我也没有设想出一个简单的解决方案

In the code above, the simple case is working as I expect. I can add B s to an A without using any session or Javascript. However I had to add a sub-route in the controller s CreateA() to check when I ve actually pressed an add button. In the more involved scenario, even more changes to the controller become necessary. The subroute will have to decide which B a new C needs to be added to.

它显示了两个案例,其中一个我已经测试过效果良好。假设使用的视图返回的html一旦发布就会被绑定回深层对象中。

The Problem
Notice, in the code, how once we are in the more involved case, the programmer is immediately stomped with how a new object should be inserted when dealing with a second level child. Specifically, I don t want to have an if condition to add a C into a specific B within the controller for A. It s annoying if I had to write the same code if List<B> existed under another parent, say D.

Is the control model totally obliterated from MVC, doesn t that mean some things are extremely harder to do?
I believe I am flirting with the need for user controls and some way to create controls that are reusable so that I can have a controller for B with its children (C s) then reuse all of that when implementing a CRUD interface for A.

There s an imbalance between the ease of creating editors for scalar types, or an object consisting of scalar types on one hand and editors for lists of objects on the other The real issue is, there are automatic editors for scalar types, but none for lists of them. The solutions that I came up with are redundant, and introduce seemingly unnecessary additional code and headache.

最后一点:我之所以这么麻烦,是因为我试图看看我能在多大程度上推动无状态状态,并保持代码的干净和在MVC模式的限制范围内。我希望有更多模式失败的地方的例子,而不是所有类似营销的模式材料。这样,我们就可以立即发现在哪里不使用某些面向模式的产品。我相信,普通的ASP.NET会使用它的视图状态和事件流模型轻松地处理我的情况

最佳回答

我不完全理解你的意思,但我觉得你是,你需要ViewModels。更确切地说,是相互继承的ViewModels。您不想一遍又一遍地编写相同的代码(DRY),因此您应该为ViewModels设计一个类模型,其中最通用的类位于顶部(即,在网站的几乎每个页面上都保存您需要的数据),而更特定于页面的类位于底部。特定于页面的类越多,继承的类就越多。

但是,如果需要频繁构建某些数据,也可以将方法添加到ViewModels中。例如,如果您需要使用选项项为选择框构建SortedDictionary,则可以在适当级别将该方法添加到类中。或者,您可以让该类始终通过构造函数来构建它。

希望这能帮助。。。

问题回答

暂无回答




相关问题
WebForms and ASP.NET MVC co-existence

I am trying to make a WebForms project and ASP.NET MVC per this question. One of the things I ve done to make that happen is that I added a namespaces node to the WebForms web.config: <pages ...

Post back complex object from client side

I m using ASP.NET MVC and Entity Framework. I m going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don t know how to do that ...

Create an incremental placeholder in NHaml

What I want to reach is a way to add a script and style placeholder in my master. They will include my initial site.css and jquery.js files. Each haml page or partial can then add their own required ...

asp.net mvc automapper parsing

let s say we have something like this public class Person { public string Name {get; set;} public Country Country {get; set;} } public class PersonViewModel { public Person Person {get; ...

structureMap mocks stub help

I have an BLL that does validation on user input then inserts a parent(PorEO) and then inserts children(PorBoxEO). So there are two calls to the same InsertJCDC. One like this=>InsertJCDC(fakePor)...

ASP.NET MVC: How should it work with subversion?

So, I have an asp.net mvc app that is being worked on by multiple developers in differing capacities. This is our first time working on a mvc app and my first time working with .NET. Our app does not ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

热门标签