2

I have a view that handles a ViewModel as below,

public class MyVM {
    public Exercise exer{ get; set; }
    public List<Category> categories { get; set; }
    public List<ExerciseInput> inputs { get; set; }
}

The view has multiple exercises based on the number of categories in the database. With a for loop i create the initial exercises for each category, the user can also add a question to each category too (as linked in has in it profile page, you can add experience to the profile), my issue is saving all this data. or updating existing data. I used Editor templates, but still can figure out how to handle multiple data for each category of the same type (ExceriseInput). Thanks in advance.

Controller code,

public ActionResult viewExcerciseDetails(int Id)
        {
            var vm = new MyVM();

            vm.exer= db.Exercise .SingleOrDefault(m => m.exerId == Id);
            vm.categories = db.Categories.OrderBy(m => m.orderNum).ToList();
            vm.inputs = db.ExerciseInput.Where(m => m.exerId == Id).ToList();

            return View(vm);
        }

A piece of code where the for loop in the view

@for (var i = 0; i < data.Count(); i++)
{
    @Html.EditorFor(m => data[i])
}
Aris
  • 19
  • 1
  • 7
  • You cannot use a `foreach` loop (refer [this answer](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943) –  May 10 '16 at 12:48
  • sorry i use a for loop, just a type, updated code above. – Aris May 10 '16 at 12:50
  • Your model is `MyVM` which does not even contain a property named `data`! Read the answer I linked to. –  May 10 '16 at 12:53
  • You are correct, data variable is just a filtered version of my model. I am looking at your posted question, thanks. – Aris May 10 '16 at 12:56
  • Don't change the code when someone is suggesting a solution or pointing out issues...creates confusion.. – SamGhatak May 10 '16 at 13:13
  • So i can have one form for rhe whole page that saves the chabged data. And how can i update the model when lets say the user adds one new excercise to a category? How would the view handle 2 kind of forms? – Aris May 10 '16 at 15:09
  • If your wanting to dynamically add and remove collection items, refer the answers [here](http://stackoverflow.com/questions/29161481/post-a-form-array-without-successful/29161796#29161796) and [here](http://stackoverflow.com/questions/28019793/submit-same-partial-view-called-multiple-times-data-to-controller/28081308#28081308) for some options –  May 11 '16 at 00:19

1 Answers1

0

Try like this
1. Change your ViewModel to the below code

public class MyVM {
    public Exercise exer{ get; set; }
    public ICollection<Category> categories { get; set; }
    public ICollection<ExerciseInput> inputs { get; set; }
}
  1. Then in the controller add the data, which is like your code.
  2. Then in the view, populate the categories and inputs by Editor Template.
  3. The Editor Template files should be in the name of respected model name like Category and ExerciseInput.
    Hope this helps
anand
  • 1,559
  • 5
  • 21
  • 45