在 MVC3 中处理创建和更新复杂数据类型的最佳方法是什么?

发布于 2024-12-13 22:07:24 字数 1204 浏览 0 评论 0原文

好吧,几天来我一直在试图找出最好的方法来做到这一点,但仍然没有想出一个非常优雅的答案,所以我希望有人能给我指出正确的方向或给予一些同行评审: )

基本上我有 3 个类(它们是不同的,并且比这些更复杂):

public class Person
{
    int ID { get; set;}
    string Name { get; set; }
    virtual IEnumerable<Place> { get; set; }

}

public class Place
{
    int ID { get; set;}
    string Name { get; set; }
    virtual IEnumerable<Thing> { get; set; }
}

public class Thing
{
    int ID { get; set;}
    string Name { get; set; }
    virtual IEnumerable<Place> { get; set; }
    virtual int PersonID { get; set; }
}

所以基本上你有 Person,他们有很多 Place,其中可以有很多 < code>Things 也可以出现在多个 Place (试图减少存储 Thing 的重复项),但仅限于该 Person

设置 ViewModel 的最佳方法是什么处理这个?我应该使用 Ajax 和 Json 自己创建所有内容(我一直在做的事情),还是有办法在 ViewModel 中处理这种类型的关系并单次发回服务器?

目前我正在执行以下操作:

填写 Person 表单 -> ajax保存到服务器,获取Person ID

填写Place表单(包括PersonID) -> ajax保存到服务器,获取Place ID

填写Thing表单(包括Person IDPlace ID在一个分隔字符串

我知道应该有一种更简单的方法来做到这一点,因为它有点庞大,但由于它是所有查询字符串,我无法弄清楚

Ok I've been trying to figure out the best way to do this for a few days, but still haven't come up with a very elegant answer so am hoping I someone can point me in the right direction or give some peer review :)

Basically I have 3 classes (they are different and much more complex than these):

public class Person
{
    int ID { get; set;}
    string Name { get; set; }
    virtual IEnumerable<Place> { get; set; }

}

public class Place
{
    int ID { get; set;}
    string Name { get; set; }
    virtual IEnumerable<Thing> { get; set; }
}

public class Thing
{
    int ID { get; set;}
    string Name { get; set; }
    virtual IEnumerable<Place> { get; set; }
    virtual int PersonID { get; set; }
}

So basically you have Persons, who have many Places, which can have many Things which can also appear in multiple Places (trying to reduce having to store duplicates of Things) but only for that Person

What is the best way to setup my ViewModel to handle this? Should I just create everything by itself using Ajax and Json (what I've been doing) or is there a way to handle this type of relationship in a ViewModel and single post back to the server?

Currently I'm doing the following:

Fill out Person form -> ajax save to server, get Person ID

Fill out Place form (including Person's ID) -> ajax save to server, get Place ID

Fill out Thing form (including Person ID and Place IDs in a delimited string

I know there should be an easier way to do this as its kinda bulky, but since its all query string I can't figure it out

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

诗化ㄋ丶相逢 2024-12-20 22:07:24

您说“有点笨重”,但我认为如果您可以使用 AJAX/JSON 在表单上实时构建对象图(可能针对 RESTful API),那么它往往会更轻量级,正如您所描述的那样。

另一种方法是使用脚本(jQuery、JSMVC、Knockout)来构建表单并立即发布整个表单。在某些情况下,在提交整个图表之前不应保留任何数据,我不得不这样做。这里的技巧是理解 ModelBinder 以及它如何为您构建/更新该图。

如果这就是您所问的问题,我可以扩展 ModelBinder 如何处理复杂对象图和集合的关键点。

You say "kinda bulky," but I think it tends to be more lightweight if you can build an object graph on a form in real time by using AJAX/JSON, probably against a RESTful API, somewhat as you describe.

The alternative is using script (jQuery, JSMVC, Knockout) to build a form and POST the whole sucker at once. I've had to do this in some situations where none of the data should be persisted until the whole graph is committed. The trick here is understanding ModelBinder and how it builds/updates that graph for you.

If this is what you were asking, I can expand on the key points of how ModelBinder deals with complex object graphs and collections.

别低头,皇冠会掉 2024-12-20 22:07:24

我回答了一个关于如何使用接口和部分视图处理这个问题的类似问题。

如何创建 Asp.Net MVC 3 视图模型

I answered a similar question about how to handle this using interfaces and partial views.

How to create Asp.Net MVC 3 view model

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文