簡體   English   中英

MVC模型綁定列表返回空對象

[英]MVC Model Binding a list returns null object

我在為屬性LookupData獲取正確的列表時遇到了麻煩,每次我回發表單時,modelbinder總是為該屬性返回null。 我認為我已經正確地完成了所有操作,因為當在formcollection中查看結果時,LookupData及其對象似乎已正確索引。

它們的命名如下,但是模型綁定器似乎不想構建列表並將其綁定回屬性:

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

lookupdata屬性的EditorTemplate

@model List<ILookupData>

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

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

} 

查找數據列表中項目的EditorTemplate

@model ILookupData

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

好吧,我通過將列表包裝在自己的類中解決了我的問題。 沒有列表作為屬性,我無法使它工作。 正如@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”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM