我无法获得正确的列表,对于属性LookupData,每次我发回一个表单时,modelbinder总是为该属性返回null。我认为我已经完成了所有事情,因为在查看formcollection中的结果时,LookupData及其对象似乎被正确编入索引。
它们的命名如下,但模型绑定器似乎不想构建列表并将其绑定回属性:
"[2].LookupData.[0].Description",
"[2].LookupData.[0].Value",
"[2].LookupData.[1].Description",
"[2].LookupData.[1].Value" etc..
我真正想要的是隐藏整个LookupData属性,并在我发布表单后将其恢复。
Reportparameter class:
public class ReportParameter
{
[RequiredIf("Required", true, ErrorMessage = "*")]
public string ParamValue { get; set; }
public string Name { get; set; }
public string Title { get; set; }
public string DefaultValue { get; set; }
public string CustomProperty { get; set; }
public string LookupQuery { get; set; }
public bool Enabled { get; set; }
public int MaxLength { get; set; }
public bool Required { get; set; }
public List<string> Dependence { get; set; }
public List<ILookupData> LookupData { get; set; }
public VariantType Type { get; set; }
public ReportType Destinations { get; set; }
}
LookupData类
public interface ILookupData
{
string Value { get; set; }
string Description { get; set; }
}
我的观点:
@model List<ReportParameter>
@section scripts{
<script>
jQuery(document).ready(function() {
$(".datepick").datepicker($.datepicker.regional["@ViewBag.LanguageCode"]);
});
</script>
}
@using (Html.BeginForm("Report", "Reports", FormMethod.Post))
{
<div id="searchpaneloptions" class="collapse in search-panel report-options">
<div class="row">
<div class="col-md-12">
<p class="pull-right lsf"><a class="close-options subtle" href="#">close</a></p>
<h2 id="options">@Metadata.Txt("Report options") <small class="option-heading">@if (ViewBag.ReportLabel != null){ @ViewBag.ReportLabel } </small></h2>
</div>
</div>
<div class="row">
@if (!Model.IsNullOrEmpty())
{
<div class="col-md-4 ">
@Html.EditorForModel()
</div>
}
</div>
<div class="clearfix space-before"></div>
</div>
}
ReportParameter的Editortemplate,我使用@ Html.EditorFor(model =&gt; model.LookupData,“Lookups”)的部分:
@model ReportParameter
@Html.HiddenFor(m => m.Name)
@Html.HiddenFor(m => m.Type)
@Html.HiddenFor(m => m.Title)
@Html.HiddenFor(m => m.CustomProperty)
@Html.HiddenFor(m => m.DefaultValue)
@Html.HiddenFor(m => m.Destinations)
@Html.HiddenFor(m => m.Enabled)
@Html.HiddenFor(m => m.LookupQuery)
@Html.HiddenFor(m => m.MaxLength)
@Html.HiddenFor(m => m.Required)
@Html.HiddenFor(m => m.Type)
@Html.ValidationSummary(true)
@{
Model.Dependence = new List<string>(){"aaa","3222","123"}; //testing string list, same problem here
}
@if (!Model.Dependence.IsNullOrEmpty())
{
@Html.EditorFor(model => model.Dependence,"Lookups2")//testing string list, same problem here
}
@if (Model.Required)
{
@Html.ValidationMessageFor(m => m.ParamValue, "*")
}
@if (Model.Type == VariantType.VT_Datetime || Model.Type == VariantType.VT_Date)
{
@Html.LabelFor(m => m.Name, Model.Title, new { @class = "xcol-lg-4 control-label" })
@Html.TextBoxFor(m => m.ParamValue, new { @class = "form-control input-lg datepick" })
}
else if (Model.Name != "destinationoptions")
{
@Html.LabelFor(m => m.Name, Model.Title, new { @class = "xcol-lg-4 control-label" })
if (Model.LookupData.IsNullOrEmpty())
{
@Html.TextBoxFor(m => m.ParamValue, new { @class = "form-control input-lg" })
}
else
{
@Html.DropDownListFor(model => model.ParamValue, Model.LookupData.ToSelectListItems(Model.ParamValue ?? Model.DefaultValue ?? ""))
@Html.EditorFor(model => model.LookupData, "Lookups")
}
}
lookupdalate为lookupdata属性
@model List<ILookupData>
@for (int i = 0; i < Model.Count; i++) {
@Html.EditorFor(m => m[i],"Lookup")
}
对lookupdata列表中的项目进行Editortemplate
@model ILookupData
@Html.HiddenFor(m => m.Description)
@Html.HiddenFor(m => m.Value)
答案 0 :(得分:0)
我通过将列表包装在自己的类中解决了我的问题。如果没有列表属性,我无法使用它。正如@Jakub指出的那样,我必须将对象类型更改为具体类而不是使用接口。
public class ReportParameterLookup
{
public IList<DatLookupData> Lookup { get; set; }
}
public class ReportParameter
{
[RequiredIf("Required", true, ErrorMessage = "*")]
public string ParamValue { get; set; }
public string Name { get; set; }
public string Title { get; set; }
public string DefaultValue { get; set; }
public string CustomProperty { get; set; }
public string LookupQuery { get; set; }
public bool Enabled { get; set; }
public int MaxLength { get; set; }
public bool Required { get; set; }
public List<string> Dependence { get; set; }
public ReportParameterLookup LookupData { get; set; }
public VariantType Type { get; set; }
public ReportType Destinations { get; set; }
}
这导致以下正确的命名“[2] .LookupData.Lookup [1] .Description”