0

I am learning some concepts in asp.net mvc. I am using entity framework and visual studio 2013 community edition. I am creating a demo app for learning. I have created model according to this link. The models are as follows. Below is course model. The course has department as foreign key. A department can have many courses.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ManyItemsDemo2.Models
{  
    public class Course
    {
        public int CourseID { get; set; }
        public string Title { get; set; }
        public string Credits { get; set; }
        public int DepartmentID { get; set; }

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

This is department model. The department model is simple. It is associated with course model.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ManyItemsDemo2.Models
{
    public class Department
    {
        public Department()
        {
            this.Cources = new HashSet<Course>();
        }
        public int DepartmentID { get; set; }
        public string Name { get; set; }
        public double Budget { get; set; }
        public string Administrator { get; set; }

        public virtual ICollection<Course> Cources { get; set; }
    }
}

There is a context class.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;

namespace ManyItemsDemo2.Models
{
    public class SchoolContext:DbContext
    {
        public SchoolContext() : base("SchoolContext") { }

        public DbSet<Course> Courses { get; set; }
        public DbSet<Department> Departments { get; set; }
    }
}

Now i have used scafolding and created controllers and views with CRUD functionality. I can create a department and courses in views. Now i need to assign multiple courses while creation of departments. So i created a this view model. Here One department has many courses.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ManyItemsDemo2.Models;

namespace ManyItemsDemo2.ViewModels
{
    public class DeptCourses
    {
        public Department Department { get; set; }
        public IEnumerable<Course> Course { get; set; }
    }

}

along with this i created a new view. Which can accept more cources while creating department. The view result is like this. The View

The plus button has a script which is from my earlier question here. The script uses jquery and clones the drop down and adds back.

The problem starts from here. When i add more than one drop down, let say 3, i am receiving null in controller, though i receives three elements, only one element has value, others are null. See the image for more clarification.

Image for clarification.

Why this happens? PS: I might overlooked real time scenarios as this is demo app for learning and clearing the concepts of one to many relationships with entity framework along with using MVC.

INDIA IT TECH
  • 1,904
  • 4
  • 10
  • 25
Gaurav Chauhan
  • 1,172
  • 3
  • 13
  • 33
  • Not sure why you accepted the answer in you previous question when it could never have worked. Its creating form controls that have no relationship at all to your model. 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 –  Apr 09 '16 at 07:43
  • But in you case it appears you only want the `CouseId` associated with you dropdownlist, in which case you view model property needs to be `IEnumerable Courses`. But a far better approach would be to display each course with an associated checkbox so the user can just select them (your current implementation would have other issues such as the user may select duplicates) –  Apr 09 '16 at 07:48
  • @StephenMuecke i tested in front end. that script was adding drop down so accepted. I am checking the links you suggested. – Gaurav Chauhan Apr 09 '16 at 07:48
  • @StephenMuecke i understand the point about duplication. Isn`t the script i used and in the answers you suggested do the same thing? Like adding a clone with fake indexer and the updating the index? – Gaurav Chauhan Apr 09 '16 at 07:55
  • No I mean the user can select Course 1, then Course 2, then Course 1. But thats only the start of the problems with your implementation. If any properties of `Course` have validation attributes, `ModelState` will be invalid. Then there is the problem of editing the object again. –  Apr 09 '16 at 07:57
  • I just need a values(Let there be duplicates.) I dont even have code for saving it to the database. I need to understand how the values are posted back while the form controls are dynamic. at break point in post method if i receive the values of dropdown, thats perfect. Thanks for pointing out about model state. Although i dont need it in this app, i have not thought about this. – Gaurav Chauhan Apr 09 '16 at 08:02
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/108687/discussion-between-stephen-muecke-and-gaurav-chauhan). –  Apr 09 '16 at 08:02

1 Answers1

0

This worked for me. Just changed the name in script and added string array in httppost.

Gaurav Chauhan
  • 1,172
  • 3
  • 13
  • 33