0

我在获取属性 LookupData 的正确列表时遇到问题,每次我发回表单时,modelbinder 总是只为该属性返回 null。我认为我做的一切都是正确的,因为 LookupData 及其对象在查看 formcollection 中的结果时似乎被正确索引。

它们的名称如下,但模型绑定器似乎不想构建列表并将其绑定回属性:

"[2].LookupData.[0].Description", 
"[2].LookupData.[0].Value", 
"[2].LookupData.[1].Description", 
"[2].LookupData.[1].Value" etc..

我真正想要的是隐藏整个 LookupData 属性并在我发回表单后将其取回。

报告参数类:

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

查找数据属性的编辑器模板

@model List<ILookupData>

@for (int i = 0; i < Model.Count; i++) { 

    @Html.EditorFor(m => m[i],"Lookup")

} 

查找数据列表中项目的编辑器模板

@model ILookupData

@Html.HiddenFor(m => m.Description)
@Html.HiddenFor(m => m.Value)
4

1 回答 1

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”

于 2013-11-01T06:37:10.730 回答