Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net mvc 4 code first: How to auto save complex model with lists of models in it?

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

Say, it's called "Create Full model test", and it will contain 100 questions, 20 questions from 5 subjects. So, 20*5=100

In the UI, creator will first select "Create full model test" from the drop down. Suppose if he select the "Full model test". He will have to select one subject among 5 subjects, then he will have to select 20 questions per subject. and then will have to save them into that "Full model test" segment.

If he selects English, he will have to select 20 questions, then he will have to select another subject, for example, physics, and will have to select another 20 questions for physics, then he will have to select maths and then will have to select another 20 questions and so on for maths. Finally 100 questions will be submitted by the form.

Now, my question is, how can I auto save the selected questions into that "Full model test" segment, so that he can continue saving 5 subject's questions before he submit the 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 Subject Model:

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 subjection 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; }
}
}
like image 986
InsParbo Avatar asked Dec 26 '22 09:12

InsParbo


2 Answers

All that you need is just to store all temporary data on client side. And when you get complete pasts or even full set of data - send it to server using POST/JSON/XML/etc.

I think it such case it will be better for you to use some kind of JavaScript frameworks, for example KnockoutJS, which with only few click's and couple lines of code will allows you to store all your data on client side in object-based model.

Anyway if you will use any framework or not, you should use JavaScript to store all of your data, including question, answers and subjects. JavaScript will allows you to store,validate and send all data that you need.

To make your work with JavaScript easier and faster - you can use jQuery library which contains functions for interaction with both DOM structure and server side.

To make learning of KnockoutJS easier just use tutorial: http://learn.knockoutjs.com

like image 198
Dmitry Zaets Avatar answered Mar 02 '23 01:03

Dmitry Zaets


This is how I am going to attempt it.

business rules: - full model test will be completed if all the subjects questions are submitted by certain user.

Assumption: we knew in advance that we have five subjects. Therefore

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

Test Poco entity structure:

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;}
...
}

Assumptions:
You can use partial views/ajax/Jquery/Angularjs/Knockout whatever suits you best in your
problem context to implement it to auto save the changes
You will add necessary view model with appropriate validation annotations/attributes
you will add validation where necessary.
you will add/update the Test entity. like for first subject you will add data into Test entity, and subsequently you will update it.

Hope it will be helpful

like image 36
aamir sajjad Avatar answered Mar 02 '23 00:03

aamir sajjad