Dictionary<short, Dictionary<EnFunction, bool>> model binding not work - c#

I could not bind the model to Controller. Please give me any advice.
Model,controller and view classes are below. When model is submmited, dictionary property equals to null.
public class GroupRights //model
{
public List<DtoGrup> groups { get; set; }
public Dictionary<short, Dictionary<EnFunction, bool>> groupRights { get; set; } // group function HasPermission
}
public enum EnFunction
{
LPDU_login,
LPDU_changePassword,
LPDU_transitList,
LPDU_PosEventList,
....
}
Controller
public ActionResult GroupRights()
{
TocCommonService.CommonServiceClient client = new TocCommonService.CommonServiceClient();
GroupRights gr = new GroupRights();
gr.groups = client.GetAllOperatorGroups().ToList();
gr.groupRights = new Dictionary<short, Dictionary<EnFunction, bool>>();
foreach (var g in gr.groups)
{
Dictionary<EnFunction, bool> permission = new Dictionary<EnFunction, bool>();
foreach (var func in Enum.GetValues(typeof(EnFunction)).Cast<EnFunction>())
{
permission.Add(func, client.hasPermission(new DtoGrup() { GROUPID = g.GROUPID }, func));
}
gr.groupRights.Add(g.GROUPID, permission);
}
return View(gr);
}
View
#model TocWebApplication.Models.GroupRights
#{
int id = 0;
}
#using (Html.BeginForm("ChangePermissionOfGroup", "Home", FormMethod.Post))
{
<table>
<thead>
<tr>
<th></th>
#foreach (var gr in Model.groups)
{
<th>#gr.GROUPNAME (#gr.GROUPID)</th>
}
</tr>
</thead>
<tbody>
#foreach (var func in Enum.GetValues(typeof(EnFunction)).Cast<EnFunction>())
{
<tr>
<td>#(func.ToString())</td>
#for (int j = 0; j < Model.groups.Count(); j++)
{
<td>#Html.CheckBoxFor(model => model.groupRights[Model.groups[j].GROUPID][func])</td>
}
</tr>
}
</tbody>
</table>
<button type="submit" class="btn btn-primary">Save changes</button>
<button class="btn">Cancel</button>
}

For all but Dictionaries where both the Key and Value are simple value types (for example Dictionary<string, bool>), the DefaultModelBinder requires the name/value attributes to be in the format (in your case)
<input name="groupRights[0].Key" value="1" ... />
<input name="groupRights[0].Value[0].Key" value="LPDU_login" ... />
<input name="groupRights[0].Value[0].Value" value="True" ... />
<input name="groupRights[1].Key" value="2" ... />
<input name="groupRights[1].Value[0].Key" value="LPDU_changePassword" ... />
<input name="groupRights[1].Value[0].Value" value="False" ... />
There are no HtmlHelper methods that can generate the correct attributes and to bind to your groupRights you would need to generate the html manually.
Instead, create a view model(s) that represents the data you want to display in the view.If I have interpreted this correctly, you want to display a table displaying each EnFunction value down, and each DtoGrup across, and to display a checkbox in each cell to determine which EnFunction is selected for each DtoGrup.
public class GroupRightsVM // represents a table cell
{
public short GroupID { get; set; }
public bool IsSelected { get; set; }
}
public class RightsVM // represents a table row
{
public RightsVM()
{
Groups = new List<GroupRightsVM>()
}
public EnFunction Right { get; set; }
public List<GroupRightsVM> Groups { get; set; } // represents the cells in a row
}
public class PermissionsVM // Represents the table
{
public PermissionsVM()
{
Permissions = new List<RightsVM>()
}
public IEnumerable<string> Headings { get; set; } // represents the table headings
public List<RightsVM> Permissions { get; set; } // represents all rows
}
And in the view
<thead>
<tr>
<th></th>
#foreach(var heading in Model.Headings)
{
<th>#heading</th>
}
</tr>
</thead>
<tbody>
#for(int i = 0; i < Model.Rows.Count; i++)
{
<tr>
<td>
#Html.HiddenFor(m => m.Permissions[i].Right)
#Html.DisplayFor(m => m.Permissions[i].Right)
</td>
#for(int j = 0; j < Model.Permissions[i].Groups.Count; j++)
{
<td>
#Html.HiddenFor(m => m.Permissions[i].Groups[j].GroupID)
#Html.CheckboxFor(m => m.Permissions[i].Groups[j].IsSelected)
</td>
}
</tr>
}
</tbody>
In the GET method, initialize an instance of PermissionsVM and populate it based on your data models and pass it to the view, for example
var groups = client.GetAllOperatorGroups()
var model = new PermissionsVM();
model.Headings = groups.Select(x => String.Format("{0} {{1})", x.GROUPNAME, x.GROUPID));
foreach (var func in Enum.GetValues(typeof(EnFunction)).Cast<EnFunction>())
{
RightsVM r = new RightsVM();
r.Right = func;
foreach (var g in groups)
{
GroupRightsVM gr = new GroupRightsVM();
gr.GroupID = g.GROUPID;
gr.IsSelected = ...
r.Groups.Add(gr);
}
model.Persmissions.Add(r);
}
return View(model);
and in the POST method
public ActionResult GroupRights(PermissionsVM model)

Related

Not able to bind value to model which is collection asp.net core mvc

I have list as model in my view. I display my items information and counts. also I have to take assignedworker to that location from the user. When I submit this method, my collectionmodel is getting null. I am losing all information in my model.
I have data in all the properties except assignedworker. I display all the information to the user using foreach and I take assignedworker name from the user. When I submit this form, List is null.
public class Report
{
public string itemname{ get; set; }
public List<itemlocation> locations { get; set; }
}
public class itemlocation
{
public string location { get; set; }
public List<items> items{ get; set; }
public string assignedworker{ get; set; }
}
View:
#model IList<Report>
<form method="post" asp-action="Report" asp-controller="Home">
#foreach (var rep in Model)
{
<tr>
<td colspan="3">
<h3>#rep.itemname</h3>
</td>
</tr>
#foreach (var loc in rep.itemlocation)
{
<tr>
<td>#loc.location </td>
<td>#loc.items.Count()</td>
<td>
<input type="text" class="form-control" id="worker" name="#loc.assignedworker" value="#loc.assignedworker">
</td>
</tr>
}
}
</form>
I have data in all the properties except assignedworker. I display all the information to the user using foreach and I take assignedworker name from the user. When O submit this form, List is null.
Expected result:
In my controller I would like to be able to see my collection(List) with all the values including assignedworker.
In order to generate the right input names for modelbinding, Razor needs the full model expression, which means you must use a regular for loop and index your lists, rather than using foreach:
#for (var i = 0; i < Model.Count; i++)
{
...
#for (var j = 0; j < Model[i].locations.Count; j++)
{
...
<input asp-for="#Model[i].locations[j].assignedworker" />
...
}
}
You could use <input asp-for="" hidden /> to pass the value to the action:
1.Model:
public class Report
{
public string itemname { get; set; }
public List<itemlocation> locations { get; set; }
}
public class itemlocation
{
public string location { get; set; }
public List<items> items { get; set; }
public string assignedworker { get; set; }
}
public class items
{
public int Id { get; set; }
public string name { get; set; }
}
2.View:
#model IList<Report>
<form method="post" asp-action="Report" asp-controller="Home">
#for (var i = 0; i < Model.Count; i++)
{
<tr>
<td colspan="3">
<input asp-for="#Model[i].itemname" hidden/>
<h3>#Model[i].itemname</h3>
</td>
</tr>
#for (var j = 0; j < Model[i].locations.Count; j++)
{
<tr>
<td>
#Model[i].locations[j].location
<input asp-for="#Model[i].locations[j].location" hidden />
</td>
<td>
#Model[i].locations[j].items.Count()
#for (var k = 0; k < Model[i].locations[j].items.Count; k++)
{
<input asp-for="#Model[i].locations[j].items[k].Id" hidden />
<input asp-for="#Model[i].locations[j].items[k].name" hidden />
}
</td>
<td>
<input asp-for="#Model[i].locations[j].assignedworker" />
</td>
</tr>
}
}
<div class="form-group">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</form>

Radio button values binding not working

I have a view:
#using(Html.BeginForm())
{
<div>
<table id="dimensionFeedbackTable">
<tbody>
#for(int i = 0; i < 6; i++)
{
<tr>
<td>
<div>
<p>
<text>
Rate #i
</text>
</p>
</div>
</td>
#foreach(var item in Model.DeptList)
{
<td>
<div style="text-align: center;">
#Html.RadioButtonFor(m => item.DepartmentValue, i,
new
{
id = i,
style = "min-height:45px;"
})
</div>
</td>
}
</tr>
}
</tbody>
</table>
<button id="submitComment" value="submit">
#{
<text>Submit</text>
}
</button>
</div>
}
Model in that view is EmployeeModelClass. Here it is:
public class EmployeeModelClass
{
public int Id
{
get; set;
}
public IEnumerable<DepartmentModelClass> DeptList
{
get; set;
}
}
public class DepartmentModelClass
{
public string DepartmentCode
{
get; set;
}
[Range(1, 6)]
public int? DepartmentValue
{
get; set;
}
}
My controller:
[HttpPost]
public ActionResult Index(EmployeeModelClass emp, IEnumerable<DepartmentModelClass> qec)
{
emp.DeptList = qec;
return View(emp);
}
I am trying to accomplish next:
Each row should be group for it self. For example Rate 1 row can have only one radio button selected. Now, these buttons should be bound. If second radio button is selected in any row then its value (DepartmentValue) should be set to 2. So when I submit, I get a collection of which radio buttons are selected.
I've tried a lot of combinations with setting proper model and binding it. Also tried html helper method RadioButton, but still no luck to return the values. Closest that I managed to get is returning a collection but although radio buttons are selected in Controller, collection is not null but values (DepartmentValues) are null.
Also, there are similar answers here but for some reason none of them is working for me. Any help and pointing direction are appreciated. Losing my mind over a simple thing.
One more thing, I tried with creating partials view but still now luck. I read online that is a bad practice to place for/foreach loop in a view and tried to simplify it but this approach is also not working.
Thanks
I notice few issues -
for loop and foreach loop are opposite.
Should use for for array of controls instead of foreach
Do not explicitly assign id to radio buttons new { id = i, ...})
View
#model DemoMvc.Models.EmployeeViewModel
#using (Html.BeginForm())
{
<div>
<table id="dimensionFeedbackTable">
<tbody>
#for (int i = 0; i < Model.DepartmentViewModels.Count; i++){
<tr>
<td>
<div>
<p>
<text>
Rate #Model.DepartmentViewModels[i].DepartmentCode
</text>
</p>
</div>
</td>
#for (int j = 1; j <= 6; j++)
{
<td>
<div style="text-align: center;">
#Html.RadioButtonFor(m =>
Model.DepartmentViewModels[i].DepartmentValue,
j, new { style = "min-height:45px;" })
#j
</div>
</td>
}
</tr>
}
</tbody>
</table>
<button id="submitComment" value="submit">Submit</button>
</div>
}
Model
public class EmployeeViewModel
{
public int Id { get; set; }
public IList<DepartmentViewModel> DepartmentViewModels { get; set; }
}
public class DepartmentViewModel
{
public string DepartmentCode { get; set; }
public int? DepartmentValue { get; set; }
}
Controller
public ActionResult Index()
{
// This could come from database.
var model = new EmployeeViewModel
{
DepartmentViewModels = new List<DepartmentViewModel>
{
new DepartmentViewModel{ DepartmentCode = "ABC", DepartmentValue = 1},
new DepartmentViewModel{ DepartmentCode = "DEF", DepartmentValue = 2},
new DepartmentViewModel{ DepartmentCode = "GHI", DepartmentValue = 3},
}
};
return View(model);
}
[HttpPost]
public ActionResult Index(EmployeeViewModel model)
{
return View(model);
}
Result
Posted Value

Passing Dictionary To ASP.NET MVC Action [duplicate]

I could not bind the model to Controller. Please give me any advice.
Model,controller and view classes are below. When model is submmited, dictionary property equals to null.
public class GroupRights //model
{
public List<DtoGrup> groups { get; set; }
public Dictionary<short, Dictionary<EnFunction, bool>> groupRights { get; set; } // group function HasPermission
}
public enum EnFunction
{
LPDU_login,
LPDU_changePassword,
LPDU_transitList,
LPDU_PosEventList,
....
}
Controller
public ActionResult GroupRights()
{
TocCommonService.CommonServiceClient client = new TocCommonService.CommonServiceClient();
GroupRights gr = new GroupRights();
gr.groups = client.GetAllOperatorGroups().ToList();
gr.groupRights = new Dictionary<short, Dictionary<EnFunction, bool>>();
foreach (var g in gr.groups)
{
Dictionary<EnFunction, bool> permission = new Dictionary<EnFunction, bool>();
foreach (var func in Enum.GetValues(typeof(EnFunction)).Cast<EnFunction>())
{
permission.Add(func, client.hasPermission(new DtoGrup() { GROUPID = g.GROUPID }, func));
}
gr.groupRights.Add(g.GROUPID, permission);
}
return View(gr);
}
View
#model TocWebApplication.Models.GroupRights
#{
int id = 0;
}
#using (Html.BeginForm("ChangePermissionOfGroup", "Home", FormMethod.Post))
{
<table>
<thead>
<tr>
<th></th>
#foreach (var gr in Model.groups)
{
<th>#gr.GROUPNAME (#gr.GROUPID)</th>
}
</tr>
</thead>
<tbody>
#foreach (var func in Enum.GetValues(typeof(EnFunction)).Cast<EnFunction>())
{
<tr>
<td>#(func.ToString())</td>
#for (int j = 0; j < Model.groups.Count(); j++)
{
<td>#Html.CheckBoxFor(model => model.groupRights[Model.groups[j].GROUPID][func])</td>
}
</tr>
}
</tbody>
</table>
<button type="submit" class="btn btn-primary">Save changes</button>
<button class="btn">Cancel</button>
}
For all but Dictionaries where both the Key and Value are simple value types (for example Dictionary<string, bool>), the DefaultModelBinder requires the name/value attributes to be in the format (in your case)
<input name="groupRights[0].Key" value="1" ... />
<input name="groupRights[0].Value[0].Key" value="LPDU_login" ... />
<input name="groupRights[0].Value[0].Value" value="True" ... />
<input name="groupRights[1].Key" value="2" ... />
<input name="groupRights[1].Value[0].Key" value="LPDU_changePassword" ... />
<input name="groupRights[1].Value[0].Value" value="False" ... />
There are no HtmlHelper methods that can generate the correct attributes and to bind to your groupRights you would need to generate the html manually.
Instead, create a view model(s) that represents the data you want to display in the view.If I have interpreted this correctly, you want to display a table displaying each EnFunction value down, and each DtoGrup across, and to display a checkbox in each cell to determine which EnFunction is selected for each DtoGrup.
public class GroupRightsVM // represents a table cell
{
public short GroupID { get; set; }
public bool IsSelected { get; set; }
}
public class RightsVM // represents a table row
{
public RightsVM()
{
Groups = new List<GroupRightsVM>()
}
public EnFunction Right { get; set; }
public List<GroupRightsVM> Groups { get; set; } // represents the cells in a row
}
public class PermissionsVM // Represents the table
{
public PermissionsVM()
{
Permissions = new List<RightsVM>()
}
public IEnumerable<string> Headings { get; set; } // represents the table headings
public List<RightsVM> Permissions { get; set; } // represents all rows
}
And in the view
<thead>
<tr>
<th></th>
#foreach(var heading in Model.Headings)
{
<th>#heading</th>
}
</tr>
</thead>
<tbody>
#for(int i = 0; i < Model.Rows.Count; i++)
{
<tr>
<td>
#Html.HiddenFor(m => m.Permissions[i].Right)
#Html.DisplayFor(m => m.Permissions[i].Right)
</td>
#for(int j = 0; j < Model.Permissions[i].Groups.Count; j++)
{
<td>
#Html.HiddenFor(m => m.Permissions[i].Groups[j].GroupID)
#Html.CheckboxFor(m => m.Permissions[i].Groups[j].IsSelected)
</td>
}
</tr>
}
</tbody>
In the GET method, initialize an instance of PermissionsVM and populate it based on your data models and pass it to the view, for example
var groups = client.GetAllOperatorGroups()
var model = new PermissionsVM();
model.Headings = groups.Select(x => String.Format("{0} {{1})", x.GROUPNAME, x.GROUPID));
foreach (var func in Enum.GetValues(typeof(EnFunction)).Cast<EnFunction>())
{
RightsVM r = new RightsVM();
r.Right = func;
foreach (var g in groups)
{
GroupRightsVM gr = new GroupRightsVM();
gr.GroupID = g.GROUPID;
gr.IsSelected = ...
r.Groups.Add(gr);
}
model.Persmissions.Add(r);
}
return View(model);
and in the POST method
public ActionResult GroupRights(PermissionsVM model)

Sent checked html table view back to client when invalid

When my Model.State is NOT valid I want to return the view WITH the checked checkboxes.
How would you change my code? Is it possible at all with my approach?
VIEW
#model ListTest.Models.PeopleListViewModel
#{
var hasMoreThanOnePerson = #Model.People.Count > 1;
}
#Html.BeginForm("Save", "Home")
{
#Html.ValidationSummary(false)
<table>
#foreach (var item in Model.People)
{
<tr>
#if (hasMoreThanOnePerson)
{
<td>
<input type="checkbox" name="SelectedIds" value="#item.PersonId" />
</td>
}
else
{
#Html.Hidden("SelectedIds", item.PersonId)
}
<td>
<input type="text" value="#item.Name" />
</td>
</tr>
}
</table>
<input type="submit" value="Save" />
}
VIEWMODEL
public class PeopleListViewModel
{
public PeopleListViewModel()
{
SelectedIds = new int[] { };
}
[MinLength(1, ErrorMessage = "Minimum one person must be selected!")]
public int[] SelectedIds { get; set; }
public List<Person> People { get; set; }
}
CONTROLLER
public ActionResult Index()
{
var people = new List<Person> {
new Person { Name = "Horst", PersonId = 10 },
new Person { Name = "Michael", PersonId = 20}
};
return View(new PeopleListViewModel { People = people });
}
[HttpPost]
public ActionResult Save(PeopleListViewModel viewModel)
{
if (ModelState.IsValid)
{
}
viewModel.People = new List<Person> { new Person { Name = "Horst", PersonId = 10 }, new Person { Name = "bernarnd", PersonId = 20 } };
return View("Index", viewModel);
}
Few things to change
Firstly, change your People model to include an IsSelected property, we want to do away with your SelectedIds method
Secondly, in order to post the data from the client, we need to rewrite your foreach to be a for so the fields are indexed correctly, we'll also add some extra HiddenFors for the properties that you want to keep (because we're no longer re-populating your model when validation fails), your table will be:
<table>
#for (int i = 0; i < Model.People.Count; i++)
{
<tr>
#Html.HiddenFor(m => m.People[i].PersonID)
#Html.HiddenFor(m => m.People[i].Name)
#if (hasMoreThanOnePerson)
{
<td>
#Html.CheckBoxFor(m => m.People[i].IsSelected)
</td>
}
else
{
#Html.HiddenFor(m => m.People[i].IsSelected)
}
<td>
<input type="text" value="#Model.People[i].Name" />
</td>
</tr>
}
</table>
Finally, we don't reassign your People list in your action method if validation fails just return the model that was passed in. If you want to get the selected people, use the code I've added below. Also, because we don't have the SelectedIds anymore we can perform our own validation:
[HttpPost]
public ActionResult Save(PeopleListViewModel viewModel)
{
List<People> selected = viewModel.People
.Where(p => p.IsSelected)
.ToList();
if (selected.Any())
{
//it's valid
List<int> selectedIds = selected
.Select(s => s.PersonID)
.ToList();
}
return View("Index", viewModel);
}

Returning viewmodel from view gives an ampty object in the controller

I'm working on a small project. The intention is to maintain the presences of a couple of users on some specific dates. This dates are first chosen by the user.
Therefor I use a view model. This will provide all possible data with a check box in a table to show to the user.
In the view itself I walk every day in the given view modeland I let the view generate a tag and an editor object (Html.EditorFor ()). All this is surrounded by the form creation (# Html.BeginForm ()). Rendering the view is not the problem.
The problem is when I try to return the view model. The viewmodel object that I receive is an object with null - objects in themselves. Like it has been created by the default constructor.
Controller:
[HttpGet]
public ActionResult OpldagenLt()
{
var lt = _gebruikerRepository.GeefTraject(_gebruikerRepository.TrajectId);
List<DatumModel> data = new List<DatumModel>();
foreach (Opleidingdag opldag in lt.GeefOpleidingdagenKleinerDanOfGelijkAanVandaag())
data.Add(new DatumModel(opldag));
List<ToekomstModel> toekomst = new List<ToekomstModel>();
foreach (Opleidingdag opldag in lt.GeefOpleidingdagenGroterDanVandaag())
toekomst.Add(new ToekomstModel(opldag));
DataModel datamod = new DataModel();
datamod.Datums = data;
datamod.Toekomst = toekomst;
if (lt.ControleerAantalOpleidingdagen())
{
_gebruikerRepository.GekozenDagen = GekozenDagen(datamod) as List<Opleidingdag>;
return RedirectToAction("Index", "Aanwezigheid");
}
return View(datamod);
}
[HttpPost]
public ActionResult OpldagenLt(DataModel model)
{
if (!ModelState.IsValid)
{
return View(model);
}
_gebruikerRepository.GekozenDagen = GekozenDagen(model) as List<Opleidingdag>;
return RedirectToAction("Index", "Aanwezigheid");
}
public ICollection<Opleidingdag> GekozenDagen(DataModel datamod)
{
List<Opleidingdag> dagen = new List<Opleidingdag>();
foreach (DatumModel datum in datamod.Datums)
{
dagen.Add(datum.Dag);
}
return dagen;
}
View:
#using(#Html.BeginForm( "OpldagenLt", "Leertraject", FormMethod.Post))
{
<div>
<table>
<tr>
<th colspan="2">reeds begonnen dagen</th>
</tr>
#foreach (var item in #Model.Datums)
{
<tr>
<td>#Html.DisplayFor(modelItem => item.Dag.Dag.Day)/#Html.DisplayFor(modelItem => item.Dag.Dag.Month)/#Html.DisplayFor(modelItem => item.Dag.Dag.Year)</td>
<td>#Html.EditorFor(modelItem => item.Checked)</td>
</tr>
}
</table>
</div>
<div id="toekomst">
<table>
<tr>
<th>Dagen in de toekomst</th>
</tr>
#foreach (var item in #Model.Toekomst)
{
<tr>
<td>#Html.DisplayFor(modelItem => item.Dag.Dag.Day)/#Html.DisplayFor(modelItem => item.Dag.Dag.Month)/#Html.DisplayFor(modelItem => item.Dag.Dag.Year)</td>
</tr>
}
</table>
</div>
<div>
<p><input type="submit" value="Volgende"/></p>
</div>
}
Model:
using System;
using System.Collections.Generic;
using ProjectII.Models.domain;
namespace ProjectII.Viewmodels
{
public class DataModel
{
public DataModel()
{
Datums = new List<DatumModel>();
}
public ICollection<DatumModel> Datums { get; set; }
public ICollection<ToekomstModel> Toekomst{ get; set; }
}
public class DatumModel
{
public DatumModel()
{
Dag = new Opleidingdag(new DateTime().Date);
Checked = true;
}
public DatumModel(Opleidingdag opldag)
{
Dag = opldag;
Checked = true;
}
public Opleidingdag Dag { get; set; }
public bool Checked { get; set; }
}
public class ToekomstModel
{
public ToekomstModel()
{
Dag = new Opleidingdag(new DateTime().Date);
}
public ToekomstModel(Opleidingdag opldag)
{
Dag = opldag;
}
public Opleidingdag Dag { get; set; }
}
}
Thanks in advance for the help
With the way you construct your view, the ModelBinder cannot differantiate between the different elements in the list, and doesn't recognize them as items in the list. Try constructing the view like this:
#{ int datumteller = 0; }
#foreach (var item in #Model.Datums)
{
<tr>
<td>#Html.DisplayFor(modelItem => #Model.Datums[datumteller].Dag.Dag.Day)/#Html.DisplayFor(modelItem => #Model.Datums[datumteller].Dag.Dag.Month)/#Html.DisplayFor(modelItem => #Model.Datums[datumteller].Dag.Dag.Year)</td>
<td>#Html.EditorFor(modelItem => #Model.Datums[datumteller].Checked)</td>
</tr>
datumteller++;
}
#{ int toekomstteller = 0; }
#foreach (var item in #Model.Toekomst)
{
<tr>
<td>#Html.DisplayFor(modelItem => #Model.Toekomst[toekomstteller].Dag.Dag.Day)/#Html.DisplayFor(modelItem => #Model.Toekomst[toekomstteller].Dag.Dag.Month)/#Html.DisplayFor(modelItem => #Model.Toekomst[toekomstteller].Dag.Dag.Year)</td>
</tr>
toekomstteller++
}
#stephen: thanks for the help.
with keeping your answer in my mind I finally succeeded returning those data.
I changed the foreach into a for
and changed ICollection to a List
Model:
using System;
using System.Collections.Generic;
using ProjectII.Models.domain;
namespace ProjectII.Viewmodels
{
public class DataModel
{
public List<DatumModel> Datums { get; set; }
public List<ToekomstModel> Toekomst{ get; set; }
}
public class DatumModel
{
public DatumModel()
{
Dag = new Opleidingdag(new DateTime().Date);
Checked = true;
}
public DatumModel(Opleidingdag opldag)
{
Dag = opldag;
Checked = true;
}
public Opleidingdag Dag { get; set; }
public bool Checked { get; set; }
}
public class ToekomstModel
{
public ToekomstModel()
{
Dag = new Opleidingdag(new DateTime().Date);
}
public ToekomstModel(Opleidingdag opldag)
{
Dag = opldag;
}
public Opleidingdag Dag { get; set; }
}
}
view:
#model ProjectII.Viewmodels.DataModel
#using(#Html.BeginForm())
{
<div>
<table>
<tr>
<th colspan="2">reeds begonnen dagen</th>
</tr>
#for (int i = 0; i < Model.Datums.Count; i++)
{
<tr>
<td>#Html.DisplayFor(modelItem => Model.Datums[i].Dag.Dag.Day)/#Html.DisplayFor(modelItem => Model.Datums[i].Dag.Dag.Month)/#Html.DisplayFor(modelItem => Model.Datums[i].Dag.Dag.Year)</td>
<td>
#Html.EditorFor(modelItem => Model.Datums[i].Checked)
</td>
</tr>
}
</table>
#*#for (int i = 0; i < Model.Datums.Count; i++)
{
<div> #Html.DisplayFor(x => Model.Datums[i].Dag.Dag)
#Html.CheckBoxFor(x => Model.Datums[i].Checked) </div>
}*#
</div>
<div id="toekomst">
<table>
<tr>
<th>Dagen in de toekomst</th>
</tr>
#foreach (var item in #Model.Toekomst)
{
<tr>
<td>#Html.DisplayFor(modelItem => item.Dag.Dag.Day)/#Html.DisplayFor(modelItem => item.Dag.Dag.Month)/#Html.DisplayFor(modelItem => item.Dag.Dag.Year)</td>
</tr>
}
</table>
</div>
<div>
<p><input type="submit" value="Volgende"/></p>
</div>
}
Now I recieve a model with the right values
thanks for the help people

Categories