当我在按下提交按钮后取回帖子时,我遇到了 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; }
}
这是我的 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");
}
}
我的索引.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 />
}
我的其他 View 位于名为 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] 的标签由空字符串填充,其他行为空。 我试过 this但我没能成功。
我有点迷路了,我不明白为什么它拒绝工作。
普克
最佳答案
您似乎在使用编辑器模板来呈现您的 Row 对象,但我认为您没有正确使用它。如果您有单个 Row 对象的编辑器模板,您只需通过将集合传递给模板来呈现 Index.cshtml 中的 Row 集合。这是您的行的编辑器模板:
@model Utils.FormObject.Row
@{
ViewBag.Title = "Row";
}
@Html.HiddenFor(x => x.Id)
@Html.EditorFor(x => x.Label)
@Html.EditorFor(x => x.Type)
从 Index.cshtml View 渲染 Row 集合 - 无需循环,因为编辑器模板会自动渲染集合中的每一行并正确命名它们:
@model Utils.FormObject.Form
@{
ViewBag.Title = "Index";
}
<fieldset>
<p>
@Html.HiddenFor(x => x.Id)
@Html.EditorFor(x => x.Rows)
</p>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
这应该创建如下所示的字段,这些字段在回传到您的 Controller 时将被正确命名并绑定(bind)为您的 Form 对象的一部分:
Rows[0].Id
Rows[0].Label
Rows[0].Type
Rows[1].Id
Rows[1].Label
Rows[1].Type
看我对这个问题的回答:
Pass values from multiple partial views
在这里写一些关于编辑器模板的小文章:
关于c# - 使用编辑器模板在 MVC4 中提交后,回发对象未正确填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11262274/