SelrekJohn
SelrekJohn

Reputation: 476

Where to store Objects MVC5

Building a user input form that can take many objects (viewmodel contains lists of other objects) that can be added and deleted (those added will be displayed in a list coming from a partial page), but I don't want the objects to be sent to the database until the final submit where all objects will be sent to the database at once.

This is to stop unnecessary additions to the database if a user leaves the form after adding some objects.

What's the best way of achieving this? I was thinking storing the model in a session updated by the controller - is this advisable?

Example Model:

public class ViewModel
{
    public string SchoolName {get;set;}
    public List<Student> student {get;set;}
    public List<Course> courses {get;set;}
}

public class Student
{
    public string Name {get;set;}
}

public class Course
{
    public string Code {get;set;}
}

Upvotes: 0

Views: 1616

Answers (2)

PurpleSmurph
PurpleSmurph

Reputation: 2107

I have used Sessions to do this and support the models between pages, reducing the need to go back to the database each time, also helps with adding lists.

With your student model in mind, I would suggest something like the below controller, you could potentially have one session, but it could get complex; I'd be more tempted to have two sessions, one for student and one for course.

    public ActionResult Index()
            {
                if (Session["StudentSession"] != null)
                {
                    List<Student> Students = (List<Person>)Session["StudentSession"];
                    // do something
                }
                else // create a new session so you can do w/e
                {
                    List<Student> Students = new List<Student>();
                    Session["StudentSession"] = Students;
                }
                return View();
            }

Upvotes: 2

Dan
Dan

Reputation: 1018

Its not advisable to store an object graph in memory using the MVC pattern. Your mention of storing data in session is one possible way of achieving what you need however you can probably simplify your model and directly persist your data on the client and in post data. This will negate the issue of multiple open tabs, exiting mid way through a workflow etc...

[HttpGet]
public ActionResult MyForm()
{
    var model = new MyFormModel();
    [...]
    return View(model);
}

[HttpPost]
public ActionResult MyForm(MyFormModel model)
{
    if (model.AddStudent)
    {
        // do some add student logic
    }

    if (model.AddCourse)
    {
        // add course logic
    }

    if (model.Submit)
    {
        // perform save
        return RedirectToAction("SomewhereElse");
    }

    return View(model);
}

View

<form method="post">

    [...]

    <button type="submit" name="AddStudent" value="True">Add Student</button>

    <button type="submit" name="AddCourse" value="True">Add Course</button>

    <button type="submit" name="Submit" value="True">Save</button>

</form>

Model

public class ViewModel
{
    [...]
    public bool AddStudent { get; set; }
    public bool AddCourse { get; set; }
    public bool Submit { get; set; }
}

This technique should continue to re-render the view and persist your data in-page until you click Save.

Upvotes: 0

Related Questions