I think I have read every post on SO about this, but still don't know why the following custom Editor Template, strongly typed to a model, is not displaying at all. It is definitely executing the Recipient.cshtml Template Editor because I put a breakpoint on the TextboxFor line and that line is hit twice, once for each Recipient Name that is set in the Controller, but when it's done, the page display is blank for the model items (submit button on main page still displays). If I uncomment the RenderPartial line, and comment out EditorFor, then the recipients display just fine, but of course the IDs are not properly enumerated so I can't post. As far as I understand I have to use the Editor Template in order for the IDs to properly enumerate and allow for the Recipients model to post.
Can anyone see what I might be doing wrong or suggest an alternative way to render this list with the correct IDs?
Thanks!
Models:
[DataContract(Name = "recipient")]
public class Recipient
{
[Display(Name = "Email")]
public string eMail { get; set; }
[Display()]
public string Name { get; set; }
}
Controller:
[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>
View:
@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>