ASP。NET MVC -复杂的对象和表单

时间:2022-04-23 17:42:26

So let's say we have a domain object such as the following

假设我们有一个域对象,如下所示

public class Person
{
   public string Name { get; set; }
   public IList<PhoneNumber> PhoneNumbers {get; set; }
   public IList<Address> Addresses { get; set; }
}

The Person is not valid until a name, phone numbers, and addresses have been entered. How do you guys handle this using ASP.NET MVC and forms...

在输入姓名、电话号码和地址之前,这个人是无效的。你们怎么用ASP处理这个呢。净MVC和形式……

I was thinking you could serialze the Person to session and have multiple views for editing Name, adding phone numbers, adding addresses - the controller actions would modify the person in the session and a final Save action would push to database.

我认为您可以将Person序列化为会话,并有多个视图用于编辑名称、添加电话号码、添加地址——控制器操作将修改会话中的Person,最后一个Save操作将推到数据库。

I don't really like having multiple views and using the session. Another option would be to have a single very complex form that could have "dynamic" sections of elements for adding/removing phone numbers, addresses within the browser prior to posting to the save action.

我不喜欢有多个视图和使用会话。另一种选择是使用一个非常复杂的表单,该表单可以包含“动态”部分,用于添加/删除电话号码,在发布保存操作之前,在浏览器中添加地址。

What is everyone doing with complex objects and editing via forms?

每个人都在使用复杂的对象并通过表单进行编辑吗?

Thanks!

谢谢!

1 个解决方案

#1


2  

I would usually use the "dynamic section" route.

我通常会使用“动态部分”路线。

However, I would not make your validation so strict that the user is unable to save work in progress. A list of phone numbers, addresses, etc., can take quite a while to enter. It is beneficial to the end-user to be able to save their work from time to time in case they lose Internet connectivity or something. It's probably a good idea to save the records automatically via AJAX from time to time if your data entry form is quite large. (Like Gmail.) Therefore, your model should allow them to save incomplete work from time to time, and run the whole validation only when they say they are "done."

但是,我不会让您的验证如此严格,以致于用户无法保存正在进行的工作。要输入一个电话号码、地址等列表,可能需要相当长的时间。对于最终用户来说,如果他们失去了互联网连接或其他什么东西,他们可以不时地保存他们的工作,这对他们是有益的。如果您的数据输入表单非常大,那么最好是通过AJAX自动保存这些记录。(如Gmail。)因此,您的模型应该允许它们不时地保存不完整的工作,并且只在它们说“完成了”时才运行整个验证。

#1


2  

I would usually use the "dynamic section" route.

我通常会使用“动态部分”路线。

However, I would not make your validation so strict that the user is unable to save work in progress. A list of phone numbers, addresses, etc., can take quite a while to enter. It is beneficial to the end-user to be able to save their work from time to time in case they lose Internet connectivity or something. It's probably a good idea to save the records automatically via AJAX from time to time if your data entry form is quite large. (Like Gmail.) Therefore, your model should allow them to save incomplete work from time to time, and run the whole validation only when they say they are "done."

但是,我不会让您的验证如此严格,以致于用户无法保存正在进行的工作。要输入一个电话号码、地址等列表,可能需要相当长的时间。对于最终用户来说,如果他们失去了互联网连接或其他什么东西,他们可以不时地保存他们的工作,这对他们是有益的。如果您的数据输入表单非常大,那么最好是通过AJAX自动保存这些记录。(如Gmail。)因此,您的模型应该允许它们不时地保存不完整的工作,并且只在它们说“完成了”时才运行整个验证。