c#-在MVC4中使用编辑器模板提交后,未正确填充回发对象

按下“提交”按钮后找回帖子时,我遇到了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)

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)
}

和ContentDropDownRow:

@model Utils.FormObject.ContentDropDown
@{
    ViewBag.Title = "ContentDropDown";
}
@Html.HiddenFor(x => x.Id)
@Html.LabelFor(x => x.Title)

我的表格显示得很好(实际上,下拉列表的标签显示的是“标签”,而不是属性的标签…).问题是当我按下提交按钮时,回填值’updatedForm’未填写.第一行Row [0]的标签由空字符串填充,其他行为null.
我尝试了this,但没有设法使它起作用.

我在这里迷路了,我不明白为什么它拒绝工作.

K

解决方法:

您似乎正在使用“编辑器模板”来呈现Row对象,但我认为您没有正确使用它.如果您具有单个Row对象的Editor模板,则只需将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视图渲染Row集合-无需循环,因为Editor模板将自动渲染集合中的每个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时,它们将被正确命名并绑定为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

在此处小写编辑模板:

codenodes.wordpress.com – MVC3 Editor Templates

上一篇:【C# MVC 5】HTML Razor 的 视图model 的 属性设置


下一篇:Blazor入手教程(八)布局Layout