我想我已经阅读了关于此的所有帖子,但仍然不知道为什么以下自定义编辑模板(强烈键入模型)根本不显示。它肯定是在执行Recipient.cshtml模板编辑器,因为我在TextboxFor行上设置了一个断点,该行被命中两次,一次为Controller中设置的每个收件人名称,但是当它完成时,页面显示模型项目为空白(主页面上的提交按钮仍然显示)。如果我取消注释RenderPartial行,并注释掉EditorFor,那么收件人显示就好了,但当然ID没有正确枚举,所以我无法发布。据我所知,我必须使用编辑器模板,以便正确枚举ID并允许收件人模型发布。
任何人都可以看到我可能做错了什么或建议使用正确的ID呈现此列表的替代方法吗?
谢谢!
型号:
[DataContract(Name = "recipient")]
public class Recipient
{
[Display(Name = "Email")]
public string eMail { get; set; }
[Display()]
public string Name { get; set; }
}
控制器:
[HttpGet]
public ActionResult CreateRecipients()
{
var recp = new List<Recipient>();
recp.Add(new Recipient());
recp[0].Name = "VTest";
recp[0].eMail = "vgtest@test.com";
recp.Add(new Recipient());
recp[1].Name = "GTest";
recp[1].eMail = "vgtest2@test.com";
return this.View(recp);
}
EditorTemplate:
@model Recipient
<div class="row">
<div class="col-md-4">
<div class="row">
<div class="col-xs-12 custom-label-left-condensed">
@Html.LabelFor(model => Model.Name)
</div>
</div>
<div class="row">
<div class="col-xs-12">
@Html.TextBoxFor(model => Model.Name)
</div>
</div>
</div>
<div class="col-md-4">
<div class="row">
<div class="col-xs-12 custom-label-left-condensed">
@Html.LabelFor(model => Model.eMail)
</div>
</div>
<div class="row">
<div class="col-xs-12">
@Html.EditorFor(model => Model.eMail, new { htmlAttributes = new { @class = "emailField" } })
</div>
</div>
</div>
</div>
查看:
@model List<Recipient>
<div class="panel panel-default">
<div class="panel-heading hidden-xs">
<h4>@ViewBag.Title</h4>
</div>
@using (Html.BeginForm())
{
<div class="container-fluid panel-body">
<div id="myData">
@if (Model != null)
{
for (int i = 0; i < Model.Count(); i++)
{
Html.EditorFor(m => Model[i], "Recipient", "Recipient[" + i + "]" );
//Html.RenderPartial("EditorTemplates/Recipient", Model[i]);
}
}
</div>
<div class="row">
<div class="col-xs-4 col-md-3 text-right">
<input id="cmdSubmit" type="submit" value="Create" class="btn btn-default btn-sm" />
</div>
</div>
</div>
}
</div>
答案 0 :(得分:0)
使用
List<recp> recps = new List<Recipients>();
recp orecp = new recp();
orecp.Name = "VTest";
orecp.eMail = "vgtest@test.com";
recps.add(orecp);
orecp = new recp();
orecp.Name = "GTest";
orecp.eMail = "vgtest2@test.com";
recps.add(orecp);
return this.View(recps);