I've got troubles with MVC4 and Editor Templates when I get the post back after press the submit button. This is my model :
public class Form
{
public Form()
{
this.Rows = new List<Row>();
}
public List<Row> Rows { get; set; }
public int Id { get; set; }
}
public class Row
{
public Row()
{
this.Label = string.Empty;
this.Type = string.Empty;
}
public string Label { get; set; }
public string Type { get; set; }
public int Id { get; set; }
}
public class SimpleRow : Row
{
public SimpleRow()
{
this.Value = string.Empty;
}
public string Value { get; set; }
}
public class DropDownRow : Row
{
public DropDownRow()
{
this.Content = new List<ContentDropDown>();
}
public List<ContentDropDown> Content { get; set; }
}
public class ContentDropDown
{
public ContentDropDown()
{
this.Title = string.Empty;
this.Selected = false;
}
public string Title { get; set; }
public bool Selected { get; set; }
public int Id { get; set; }
}
This is my controller :
public class FormController : Controller
{
[Authorize]
public ActionResult Index()
{
return this.View();
}
[HttpPost]
[Authorize]
public ActionResult Index(HttpPostedFileBase file)
{
this.ViewBag.Title = "Formulaire Collaborateur";
if (file != null && file.ContentLength > 0)
{
return this.View(SerialisationHelper.DeserializeFromStream<Form>(file.InputStream));
}
return this.View();
}
[HttpPost]
[Authorize]
public ActionResult EmailForm(Form updatedForm)
{
Form f = updatedForm; // Here, The parameter updatedForm have just is first row filled with empty strings and the other rows null
return this.View("Index");
}
}
My Index.cshtml :
@model Utils.FormObject.Form
@{
ViewBag.Title = "Index";
}
@using (Html.BeginForm("EmailForm", "Form", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<fieldset>
<p>
@for (int i = 0; i < Model.Rows.Count; i++)
{
@Html.HiddenFor(x => x.Id)
@Html.EditorFor(x => x.Rows[i])
}
</p>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
<br />
}
my others views in a sub-folder called EditorTemplates : Row :
@model Utils.FormObject.Row
@{
ViewBag.Title = "Row";
}
@Html.HiddenFor(x => x.Id)
@Html.EditorFor(x => x)
SimpleRow :
@model Utils.FormObject.SimpleRow
@{
if (Model.Type.Equals("String"))
{
<br />
@Html.HiddenFor(x => x.Id)
@Html.DisplayFor(modelItem => modelItem.Label)
@Html.EditorFor(modelItem => modelItem.Value)
}
<br/>
}
DropDownRow :
@model Utils.FormObject.DropDownRow
@{
ViewBag.Title = "DropDownRow";
}
@* @Html.DropDownListFor(x => x, new SelectList(Model.Content, "Title", "Title"))*@
@{
@Html.HiddenFor(x => x.Id)
@Html.LabelFor(item => item.Label)
var list = new List<SelectListItem> { new SelectListItem { Value = string.Empty, Text = "--please select--", Selected = false } };
foreach (var row in Model.Content)
{
list.Add(new SelectListItem { Text = row.Title, Selected = row.Selected });
}
@Html.DropDownListFor(x => x, list)
}
and ContentDropDownRow :
@model Utils.FormObject.ContentDropDown
@{
ViewBag.Title = "ContentDropDown";
}
@Html.HiddenFor(x => x.Id)
@Html.LabelFor(x => x.Title)
My Form is approximately well displayed (in fact the label of the dropdown lists are displaying 'Label' instead of the Label of the property ...). The problem is when I press the submit button the post back value 'updatedForm' is not filled. The first row Row[0] have its labels filled by empty strings and the others rows are null. I tried this but I didn't managed to make it works.
I'm a little lost here, I don't understand why it refuse to work.
PuK