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
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
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
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Company company)
{
if (ModelState.IsValid)...
{
The controller GET Method
[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
<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"> </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"} }) @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.