Reputation: 731
Im wanting to create an editor for a List of Models which exists as a property in my ViewModel class.
ViewModel class:
public class FooManagementDetailViewModel : ViewModelBase
{
public List<FooPermissionModel> FooPermissions { get; set; }
Model class:
public class FooPermissionModel
{
public string Name { get; set; }
public string Reason { get; set; }
public bool Selected { get; set; }
}
EditorTemplate:
@model FooPermissionModel
<table>
<thead>
<tr>
<th>
Heading
</th>
<th>
Heading
</th>
<th>
Heading
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
@Html.TextBoxFor(x => x.Name, new { @readonly = "readonly" })
</td>
<td>
@Html.CheckBoxFor(x => x.Selected)
</td>
<td>
@Html.TextBoxFor(x => x.Reason)
</td>
</tr>
</tbody>
</table>
View:
<fieldset>
<legend>FooBarTitle</legend>
<div>
@Html.EditorFor(x => x.FooPermissions)
</div>
</fieldset>
What im being returned is a single div of the Names only. No structure at all. What am i missing?
Thanks!
Upvotes: 2
Views: 2582
Reputation: 8393
I have a feeling you may have not specified either your model name correctly or perhaps your EditorTemplate is not being found by MVC.
Note, that the template location for this example is: ~/Views/Shared/EditorTemplates/FooPermissionModel.cshtml
The following shows the EditorTemplate rendering correctly:
Model:
public class FooPermissionModel
{
public string Name { get; set; }
public string Reason { get; set; }
public bool Selected { get; set; }
}
ViewModel:
public class FooManagementDetailViewModel
{
public List<FooPermissionModel> FooPermissions { get; set; }
}
Controller:
public ActionResult Index()
{
var fakePerms = new List<FooPermissionModel> ()
{
new FooPermissionModel { Name = "Foo1", Reason = "Boo", Selected=true },
new FooPermissionModel { Name = "Bazz", Reason = "Tootsie", Selected=false }
};
var model = new FooManagementDetailViewModel();
model.FooPermissions = fakePerms;
return View(model);
}
View:
@model StackExamples.Models.FooManagementDetailViewModel
<fieldset>
<legend>FooBarTitle</legend>
<div>
@Html.EditorFor(x => x.FooPermissions)
</div>
</fieldset>
EditorTemplate:
@model StackExamples.Models.FooPermissionModel
<table>
<thead>
<tr>
<th>
Heading
</th>
<th>
Heading
</th>
<th>
Heading
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
@Html.TextBoxFor(x => x.Name, new { @readonly = "readonly" })
</td>
<td>
@Html.CheckBoxFor(x => x.Selected)
</td>
<td>
@Html.TextBoxFor(x => x.Reason)
</td>
</tr>
</tbody>
</table>
Upvotes: 2
Reputation: 13865
Try using EditorForModel()
View:
@model FooPermissionModel
@using (Html.BeginForm("bar", "foo"))
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Edit</legend>
@Html.EditorForModel()
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
Controller:
public ActionResult Edit()
{
//get instance of model
// <CODE GOES HERE>
// then pass it in the return
return View(mymodelinstance);
}
Using a correct template, it should do the rest for you and place it into a table.
Upvotes: 1
Reputation: 2688
Your @model should be FooManagementDetailViewModel and then you can foreach the FooPermissions.Items which will be FooPermissions objects
Upvotes: 1