Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 2 tables in my SQL database (Companies, Sectors). The 'Companies' is the master and the 'Sectors' is the detail one. A foreign key join the Sectors.CompanyID to Companies.Id. There is also, a table named 'StaticSectors' that contain all the static Sector values that a user may select, in an inner join manner. For example a staticSector (Id: 1, Name: SectorA, ParentTreeViewID: 0) is the Parent of the following Sector row (Id: 2, Name: SectorB, ParentTreeViewID: 1)
So, the schema of the tables are
- Companies (Id, name, Description)
- Sectors (Id, StaticSectorID, CompanyID)
- StaticSectors (Id, TreeViewName, ParentTreeViewID)

At the MVC side, I have the associated Models, a Controller [CompaniesController] and a View Create for Company.

I create a form (the above view), using a TreeView implementation, in order to select the internet user the sectors of the company that will register. The issue is that the binding of the detail table (sectors) cannot been succesfully. Please help!!!

What I have tried:

The Company Model

C#
public class Company
    {
        public Company()
        {
            StaticSectorsViewModel = new List<StaticSectorViewModel>();
        }
        public int Id { get; set; }

        public string Name { get; set; }

        public string Description { get; set; }

        public IList<StaticSectorViewModel> StaticSectorsViewModel { get; set; }
}


The StaticSectorViewModel

C#
public class StaticSectorViewModel
   {
       public int Id { get; set; }
       public string TreeViewName{ get; set; }
       public int ParentTreeviewID { get; set; }
       public bool IsChecked { get; set; }
    }


The controller POST method

C#
[HttpPost]
       [ValidateAntiForgeryToken]
       // I have already try that
       //public ActionResult Create([Bind(Include = "Id,Name,Description,StaticSectorsViewModel")] Company company)
       public ActionResult Create(Company company)
       {
// ISSUE HERE: The company.StaticSectorsViewModel IS NULL and i cannot retrieve the selected sectors in order to Save....
           if (ModelState.IsValid)...
           {


The controller GET Method

C#
[HttpGet]
       public ActionResult Create()
       {
           Company company = new Company();

           db.StaticSectors.ToList().ForEach(listItem => company.StaticSectorsViewModel.Add(new StaticSectorViewModel{
               Id = listItem.Id,
               TreeViewNameEN = listItem.TreeViewNameEN,
               TreeViewNameGR = listItem.TreeViewNameGR,
               ParentTreeviewID = listItem.ParentTreeviewID,
               IsChecked = false
           }));

return View(company);
       }


The Create View Code Segment

HTML
<div class="treeview">
    @if (Model.StaticSectorsViewModel != null && Model.StaticSectorsViewModel.Count() > 0)
    {
        <ul class="list-group">
            @foreach (StaticSectorViewModel currentSector in StaticSectorsViewModel.Where(current => current.ParentTreeviewID == 0).ToList())
            {
                <li class="text-info list-group-item">
                    @if (currentSector.ParentTreeviewID == 0)
                    {
                        <span class="collapse collapsible">&nbsp;</span>
                        <span>
                            <label><strong style="color: #0a0826">@currentSector.TreeViewNameEN</strong></label>
                        </span>
                        <ul>
                            @foreach (StaticSectorViewModel currentSubSector in StaticSectorsViewModel.Where(current => current.ParentTreeviewID == currentSector.Id).ToList())
                            {
                                <li class="text-info list-group-item">
                                    <label>
                                        @Html.EditorFor(model => model.StaticSectorsViewModel.Where(x => x.Id == currentSubSector.Id).First().IsChecked, new { htmlAttributes = new { @value = currentSubSector.Id, @style = "color: #333333"} }) &nbsp; @currentSubSector.TreeViewName

@* I try the following code without success *@
@*@Html.CheckBox(currentSubSector.Id, new { value = currentSubSector.Id, @checked = currentSubSector.IsChecked });*@
                                        @*@Html.CheckBoxFor(model => currentSubSector.IsChecked, new { @id = currentSubSector.Id.ToString(), name = currentSubSector.Id, style = "color: #333333" })*@

                                    </label>
                                </li>
                            }
                        </ul>
                    }
                </li>
            }
        </ul>


I think that the issue is in View, the model is passed full of data but when Post the data of Model.StaticSectorVsViewModel are Lost. The Primary data such as Company name, description are OK.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900