我想为模型列表创建一个编辑器,该编辑器作为属性存在于我的ViewModel类中。
ViewModel类:
public class FooManagementDetailViewModel : ViewModelBase
{
public List<FooPermissionModel> FooPermissions { get; set; }
型号类别:
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>
视图:
<fieldset>
<legend>FooBarTitle</legend>
<div>
@Html.EditorFor(x => x.FooPermissions)
</div>
</fieldset>
即时消息返回的只是名称的一个div。根本没有结构。
我想念什么?
谢谢!
最佳答案
我觉得您可能没有正确指定模型名称,或者MVC找不到您的EditorTemplate。
请注意,此示例的模板位置为:〜/ Views / Shared / EditorTemplates / FooPermissionModel.cshtml
以下内容正确显示了EditorTemplate呈现:
模型:
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; }
}
控制器:
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);
}
视图:
@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>