抱歉,如果之前已经回答过这个问题,我找不到任何与我的问题相符的内容。
我有一个视图,其中包含一个部分视图,并且在提交视图时,部分视图中的数据不会发送到控制器。我知道如果我使用编辑器模板,这应该可以工作,但我无法使用它,因为我需要来自部分内部主模型的数据。
我的创建视图(简化):
@model CreateXmlSchemaModel
@using (Html.BeginForm("Create", "XmlSchema", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<fieldset>
<div class="editor-label">
@Html.LabelFor(m => m.XmlSchema.Name)
</div>
<div class="editor-field">
@Html.EditorFor(m => m.XmlSchema.Name)
</div>
<div id="fieldmapping">
@Html.Partial("_XmlFieldMapping")
</div>
<div>
<input type="submit" value="Create" class="button" />
</div>
</fieldset>
}
我的模型结构(简化):
public abstract class XmlSchemaModelContainerBase
{
public IEnumerable<string> BusinessObjects { get; set; }
public IEnumerable<XmlInputFieldModel> XmlFields { get; set; }
public IEnumerable<string> BusinessObjectFields { get; set; }
}
public class XmlSchemaModelContainer : XmlSchemaModelContainerBase
{
public XmlSchemaModel XmlSchema { get; set; }
}
public class XmlSchemaModel
{
public Guid Id { get; set; }
public string Name { get; set; }
public string BusinessObject { get; set; }
public IEnumerable<XmlSchemaField> MappedFields { get; set; }
}
public class XmlSchemaField
{
public string XmlElement { get; set; }
public string BusinessObjectField { get; set; }
}
public class XmlInputFieldModel
{
public string XmlElement { get; set; }
public string XmlValue { get; set; }
}
public class CreateXmlSchemaModel : XmlSchemaModelContainer
{
[Required(ErrorMessage = "File upload required")]
public HttpPostedFileBase Xml { get; set; }
}
我的控制器(我不会全部发布,因为我可以立即知道我的 viewModel 不包含 MappedFields):
[HttpPost]
public ActionResult Create(CreateXmlSchemaModel viewModel)
{
}
我的 _XmlMappedFields 部分视图:
@model CherwellXmlConnector.Models.XmlSchemaModelContainer
@{
Model.XmlSchema.MappedFields = Model.XmlSchema.MappedFields.OrderBy(i => i.XmlElement);
}
<div>
<div>
@for(int i = 0; i < @Model.XmlFields.Count(); i ++)
{
List<CherwellXmlConnector.Models.XmlSchemaField> fields = Model.XmlSchema.MappedFields.ToList();
string itemValue = Model.XmlFields.FirstOrDefault(x => x.XmlElement == fields[i].XmlElement).XmlValue;
<div style="width: 300px; display: inline-block;">
@Html.DisplayFor(x => fields[i].XmlElement)
</div>
<div style="width: 300px; display: inline-block;">
@itemValue
</div>
<div class="busoblist" style="display: inline-block;">
@Html.DropDownListFor(x => fields[i].BusinessObjectField, new SelectList(Model.BusinessObjectFields))
</div>
<hr />
}
</div>
</div>
我的目的是在部分中向用户显示所有给定 XmlField 及其值的列表,并允许他们从每个 XmlField 的下拉列表中选择 BusinessObjectField。然后我希望将其提交回控制器内IEnumerable<XmlSchemaField> MappedFields
目的。这可能吗?