0

This example I have been struggling with best practice for a while and I cannot seem to get it right. Here is the model that I am trying to accomplish on the view and then bind it to a model in a post. It is a test a test has several attributes as well as a collection of testItems (one-to-many relationship)

enter image description here

The code of the models for the above form would be as so:

public class Test
{
    public int TestID { get; set; }
    public virtual ICollection<TestItem> TestItems { get; set; }
}

public class TestItem
{
    public int TestID { get; set; }
    public int TestItemScore { get; set; }
    public int? TestID { get; set; }
    public virtual Test Test { get; set; }
}

Currently My methods for posting the form would be to make a post method

that takes a model and then two arrays, one of questions and one of answers, then those are made into objects in the post function and then added to the model that I passed to the function like so.

public ActionResult PostATest (Test test, string[] questions, string[] answers){
   //create a list of test.testItems
   //create a new test item class
   //loop over the array of questions and add an answer and question to the test item class
   //add the test item to the test
   //repeat until all questions and answers are added to new object


   //save the test object to the db context.
   db.tests.save(test);
}

So the code above is how I have been doing most of these general practices, but there are serious problems with this, especially when going back and trying to make a model that I can edit.

Most of the html has to be done by hand using similar variable names to make the string arrays bind-able.

Ideally I would like to have a function in my post that is simply (the function below) that would know that there are many to one models inside that one model. It needs to be flexible so that when the form is submitted that it automatically creates the test Model and gives it to the post and it also creates the testItem model.

 public actionResult testPost(Test test){

    }

or at the very least have a post that posts like this maybe

public actionResult testPost(Test test, TestItem[] testItem){

}

I have been doing this the way that the first post method above suggests for about a month and I cannot seem to find ANY other way to do this without making each item on the test its own form.

A thorough way to solve this problem would be greatly appreciated. The code provided is just example code, if some of my actual implemented code is required, just ask. I have done this in several different contexts and there has to be a better way to tackle this problem.

The biggest problem with the way that I am currently doing it is that it makes it tremendously difficult to go back and edit a form later with the way that I am doing it now.

Please note any questions that you may have on how I have asked my question in the comments or if there is anything that I need to make a little more clear.

Travis Tubbs
  • 827
  • 1
  • 14
  • 32
  • If the model in the view is `Test`, then that is all that is required in the POST method i.e. it should be just `public ActionResult PostATest (Test model)`. Where do `question` and `answer` come into it? - there are no properties in your model(s) relating to that –  Nov 30 '16 at 00:45
  • And to correctly generate you view for collection items so that is is bound to your model when you submit you need to use a `for` loop or custom `EditorTemplate` for typeof `TestItem`, refer [this answer](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943) –  Nov 30 '16 at 00:48
  • That is the part that I am unsure of. at the top of my view, I HAVE been using '@model myproject.models.test' to access my model. Currently I just have text boxes with the same name for question and then the same for answer, then I create them into a class IN the post method then add them to test. My question really means, how do I handle having both models on the view, I guess. – Travis Tubbs Nov 30 '16 at 00:50
  • See the answer I linked to (and stop using data models in your view - create view models, especially when editing) –  Nov 30 '16 at 00:51
  • I am reading it now. So in regards to that answer, he uses LeaveDetailsList[index}.attribute, Does he HAVE to use a view model for that? or can you just do that right away if your model already has a list attribute? Why shouldnt I use data models? and what exactly is the difference? – Travis Tubbs Nov 30 '16 at 00:54
  • I am not fully understanding how the binder knows the difference between when I say @model at the top and when i say Model in the razor syntax. I cannot find anywhere that really shows the difference. would the view model here be the same as the data model in this case? excepts for having validation components of course. – Travis Tubbs Nov 30 '16 at 00:57
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/129387/discussion-between-stephen-muecke-and-travis-tubbs). –  Nov 30 '16 at 01:08
  • @StephenMuecke I am wrapping back around on this topic once more, but in WPF this time. Would you happen to be able to direct me to a resource where I do a similar action as this where I have a Model with a list of models inside of it? – Travis Tubbs Jun 05 '17 at 14:12
  • 1
    Its been years since I have done anything with WPF, so cant really help. –  Jun 05 '17 at 21:58

0 Answers0