当我在按下提交按钮后收到帖子时,我遇到了 MVC4 和编辑器模板的问题。这是我的模型:
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; }
}
这是我的控制器:
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");
}
}
我的 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 />
}
我的其他人在名为 EditorTemplates 的子文件夹中查看:行:
@model Utils.FormObject.Row
@{
ViewBag.Title = "Row";
}
@Html.HiddenFor(x => x.Id)
@Html.EditorFor(x => x)
简单行:
@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/>
}
下拉行:
@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)
}
和 ContentDropDownRow :
@model Utils.FormObject.ContentDropDown
@{
ViewBag.Title = "ContentDropDown";
}
@Html.HiddenFor(x => x.Id)
@Html.LabelFor(x => x.Title)
我的表单显示得差不多(实际上下拉列表的标签显示的是“标签”而不是属性的标签......)。问题是当我按下提交按钮时,回发值“updatedForm”未填写。第一行 Row[0] 的标签由空字符串填充,其他行为空。我试过这个,但我没有设法让它工作。
我在这里有点迷路,我不明白为什么它拒绝工作。
普克