简体   繁体   English

MVC 复杂模型绑定到列表<T>

[英]MVC Complex Model Binding to List<T>

I am a new noob at c# MVC and I would really like some help if possible or if someone could kindly point me in the right direction.我是 c# MVC 的新手,如果可能的话,我真的很想得到一些帮助,或者如果有人可以指点我正确的方向。 I have spent hours and hours looking for a solution online and I am yet to find anything helpful which is why I am posting here.我花了好几个小时在网上寻找解决方案,但我还没有找到任何有用的东西,这就是我在这里发帖的原因。

I am working on creating an employee database web application for my company and I have for example the following classes.我正在为我的公司创建一个员工数据库 Web 应用程序,例如我有以下类。

namespace my_app.Models.Employee
{
    public class Employee
    {

        public Employee()
        {
        }

        // id
        public int id { get; set; }

        // employee id/payroll no
        public string Employeeid { get; set; }

        // employee qualifications
        public IList<EmployeeQualification> EmployeeQualifications { get; set; }
    }
}


    namespace my_app.Models.Employee
    {
    public class EmployeeQualification
        {
            public EmployeeQualification()
            {
            }

            // employee Qualification id
            [Key]
            [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
            public int Id { get; set; }

            // employee Qualification institution

            public string Institution { get; set; }

            // employee Qualification qualificaiton

            public string Qualification { get; set; }

            public IList<Employee> Employees { get; set; }

        }
    }

The employee is being added to an EmployeeViewModel which looks something like this:员工被添加到一个 EmployeeViewModel 中,它看起来像这样:

namespace my_app.ViewModels
{
    public class EmployeeViewModel
    {

        public Employee Employee { get; set; }

        // more stuff here
    }
}

Now the problem I have and that I need help with is I would like to display the employee (ie Name, Address etc) in a view but also be able to list out the employee's qualifications with the ability to add and delete qualifications.现在我遇到并且需要帮助的问题是我想在视图中显示员工(即姓名、地址等),但也能够列出员工的资格,并能够添加和删除资格。

I have seen lots of tutorials out on the web of how to do this via AJAX and Entity framework SaveChanges method and i am sure that will work but the slightly complicated part with what i want to do is that i would like for the changes to remain on the client and only when the whole form including any changes to the employee object is submitted then the changes are persisted to the database.我在网上看到了很多关于如何通过 AJAX 和实体框架 SaveChanges 方法执行此操作的教程,我确信这会起作用,但我想要做的稍微复杂的部分是我希望更改保留在客户端上,并且只有当整个表单包括对员工对象的任何更改都被提交时,这些更改才会持久保存到数据库中。

Is there any way to do this with standard mvc controls or do i have to write a ton of JavaScript to save the changes to a local array of objects and then on the form submit append the additional form data.有没有办法用标准的 mvc 控件来做到这一点,或者我是否必须编写大量的 JavaScript 来保存对本地对象数组的更改,然后在表单上提交附加表单数据。

I did write a whole bunch of jQuery to kind of get it to work but the issue is that I need to have this functionality on multiple views of the application and to have to write that much code each time does not seem smart.我确实写了一大堆 jQuery 来让它工作,但问题是我需要在应用程序的多个视图上拥有这个功能,并且每次都必须编写那么多代码似乎并不聪明。

Any help would be greatly appreciated, thanks.任何帮助将不胜感激,谢谢。

Hope I can give you a direction to move forward.希望我能给你一个前进的方向。

Firstly, you need to change your Models design.首先,您需要更改模型设计。 Basically, 1 employee can have many qualifications and 1 qualification can owned by many employees.基本上,1 个员工可以拥有多个资格,而 1 个资格可以由多个员工拥有。 So you have many-to-many relationship here.所以你在这里有多对多的关系。 Which means you need 3 models (Employee, EmployeeQualification, Qualification).这意味着您需要 3 个模型(Employee、EmployeeQualification、Qualification)。 The 2nd model will hold foreign keys to employee and qualification.第二个模型将持有员工和资格的外键。

Next thing is UI, you want to keep all changes in UI before doing only 1 submit to persist data.接下来是 UI,您希望在仅执行 1 次提交以持久化数据之前保留 UI 中的所有更改。 That's actually a very good idea in term of user friendly system design.就用户友好的系统设计而言,这实际上是一个非常好的主意。 To do this, you just need to maintain a list of SelectedQualificationIds.为此,您只需要维护一个 SelectedQualificationIds 列表。 Then keep that list in a hidden field, so after submitting, you can just load all qualitifications from DB, compare with the list, and remove/add qualification accordingly.然后将该列表保留在隐藏字段中,以便在提交后,您只需从数据库加载所有资格,与列表进行比较,并相应地删除/添加资格。

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

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