Asp.net mvc 4 code first: How to automatically save a complex model with lists of models in it?

I am creating a quiz creator module. There are 5 items. Each subject has 2000 questions.

Say it’s called “Create a complete model test” and it will contain 100 questions, 20 questions from 5 subjects. So 20 * 5 = 100

In the user interface, the creator will first select Create Full Model Sample from the drop-down list. Suppose if he selects "Full Model Test." He will need to choose one subject from 5 subjects, then he will need to choose 20 questions for each subject. and then you’ll have to save them in the “Full Model” segment.

If he chooses English, he will need to choose 20 questions, then he will have to choose another subject, for example physics, and he will need to choose 20 more questions for physics, then he will have to choose math, and then you will need to choose 20 more questions and t .d. for math. Finally, the form is filled with 100 questions.

Now, to my question, how can I automatically save selected questions in this "Full Model" segment so that he can continue to save 5 subject questions before submitting a form.

Here is my department model:

namespace MvcBCS.Models
{
public class Department
{
    [Key]
    public int DepartmentId { get; set; }
    public string DepartmentName { get; set; }
    public string DepartmentCode { get; set; }

    public virtual ICollection<Subject> Subject { get; set; }
    public virtual ICollection<Section> Section { get; set; }
    public virtual ICollection<Subsection> Subsection { get; set; }
}
}

Here is my topic:

namespace MvcBCS.Models
{
public class Subject
{
    [Key]
    public int SubjectId { get; set; }
    public string SubjectName { get; set; }
    public string SubjectCode { get; set; }

    public int DepartmentId { get; set; }
    public virtual Department Department { get; set; }

    public virtual ICollection<Section> Section { get; set; }
}
}

Here is the section model:

namespace MvcBCS.Models
{
public class Section
{

    [ForeignKey("Department")]
    public int? DepartmentId { get; set; }
    public virtual Department Department { get; set; }

    [ForeignKey("Subject")]
    public int? SubjectId { get; set; }
    public virtual Subject Subject { get; set; }

    [Key]

    public int SectionId { get; set; }

    public string SectionName { get; set; }


}
}

Here is the submission model:

namespace MvcBCS.Models
{
public class Subsection
{

    [ForeignKey("Department")]
    public int? DepartmentId { get; set; }
    public virtual Department Department { get; set; }

    [ForeignKey("Subject")]
    public int? SubjectId { get; set; }
    public virtual Subject Subject { get; set; }

    [ForeignKey("Section")]
    public int? SectionId { get; set; }

    public virtual Section Section { get; set; }

    [Key]
    public int SubsectionId { get; set; }
    public string SubsectionName { get; set; }
    public string SubsectionCode { get; set; }
}
}
+4
source share
2 answers

, , . - POST/JSON/XML ..

, - JavaScript, KnockoutJS, - .

, - , JavaScript , , . JavaScript , .

JavaScript, jQuery , DOM, .

KnockoutJS, : http://learn.knockoutjs.com

+8

.

-: - , .

: , .

public enum Subject
{
    English,
    Physics,
    ...
}

Poco:

public class Test
{
public int Id{get;set;}
public User CreatedBy{get;set;}
public Subject Subject{get;set;}
public bool IsFullTestCompleted{get;set;}

public string Question1{get;set;}
public string Question2{get;set;}
...
}

:
/ajax/Jquery/Angularjs/Knockout,
,
/
.
/ . , , .

,

+2

Source: https://habr.com/ru/post/1538998/


All Articles