0

带有简单控制器的 Mvc 应用程序,我在其中返回带有模型的视图。进入你创建视图。由于大量的下拉列表,我有几个 viewbags(13)。

在视图中,我使用编辑器模板来显示模型中的复杂对象。

问题是编辑器模板中有几个视图包是空的。

如果我让调试器在控制器中停止,我的 viewbags 都填充正确。

如果我让调试器在视图中停止,我可以看到其中一些是空的。

wath会导致这个问题吗?

控制器:

    public ActionResult SetNewInputForm(InitializeNewInputViewModel model)
    {

       var Input = new Input(model.AantalPrimairen,model.AantalSecundairen);
        ViewBags();
        return View(Input);
    }

       private void ViewBags()
    {
        ViewBag.Blikvorm = Enum.GetNames(typeof(Common.BlikVorm)).ToList();
        ViewBag.Kwaliteit = Database.Kwaliteit.ToList();
        ViewBag.BlikFabrikanten = Database.BlikFabrikant.ToList();
        ViewBag.Walserijen = Database.Walserij.ToList();
        ViewBag.BlikMontage = new SelectList(Database.KernMontage.ToList(),  "Id", "KernMontageNaam");
        ViewBag.SchakelWijze = Enum.GetNames(typeof(Common.SchakelWijze)).ToList();        
        ViewBag.AantalGaten = Enum.GetNames(typeof(Common.AantalGaten)).ToList();
        ViewBag.DiameterGaten = Database.DiameterGat.ToList();
        ViewBag.BlikFormaat = Database.BlikFormaat.ToList();
        ViewBag.KernMontage = Database.KernMontage.ToList();
        ViewBag.KokerFabrikanten = Database.KokerFabrikant.ToList();
    }

我的一部分观点

@model DomainClasses.Input


@using (Html.BeginForm("Create", "Input", FormMethod.Post))
{
<div class="containter">
    <div class="row">
        <div class="col-lg-4 ">
            <div class="form-group">
                <strong> @Html.DevExpress().LabelFor(t => t.SchakelwijzePrimair).GetHtml()</strong>
                @Html.DevExpress().ComboBoxFor(t => t.SchakelwijzePrimair,
    settings =>
    {
        settings.Name = "SchakelwijzePrimair";

    }).BindList(ViewBag.Schakelwijze).GetHtml()

            </div>
            @Html.EditorFor(m => m.Primairen, Model.Primairen);
        </div>
        <div class="col-lg-8">

            @Html.EditorFor(m => m.Secundairen, Model.Secundairen);
        </div>

    </div>
</div>
<div class="containter">
    <div style="padding:1em;
         margin:10px 5px;
        border-style:inset;
        background-color: aquamarine;
         overflow:auto">
        @Html.EditorFor(m => m.BlikAfmeting, Model.BlikAfmeting)
    </div>
</div>

我的编辑器模板的一部分:

@model DomainClasses.BlikAfmeting


<h4 class="modal-title"> Blik Afmetingen</h4>
<div class="col-lg-2">
<div class="form-group">

        @Html.DevExpress().Label(settings =>
{
   settings.Name = "ChooseBlikAfmetingComboLabel";
   settings.Text = "Blikformaten";
}).GetHtml()
        @Html.DevExpress().ComboBox(
  settings =>
  {
    settings.Name = "ChooseBlikAfmetingCombo";
    settings.Properties.DropDownStyle = DropDownStyle.DropDownList;
    settings.Properties.CallbackPageSize = 15;
    settings.Properties.IncrementalFilteringMode =  IncrementalFilteringMode.StartsWith;
    settings.Properties.TextFormatString = "{0} {1}";
    settings.Properties.ValueField = "Id";
    settings.Properties.NullText = "Kies BlikFormaat";
    settings.Properties.ValueType = typeof(string);
    //settings.Properties.ClientSideEvents.ValueChanged = "function(s, e) { GetBlikAfmetingTabel(s,e); }";
    settings.Properties.Columns.Add("BlikVorm", "Blik Vorm", 90);
    settings.Properties.Columns.Add("HoofdAfmeting", "Hoofd Afmeting", 90);

}
 ).BindList(ViewBag.BlikFormaat).GetHtml()

Viewbag.Blikformaat 始终为空 Viewbag.BlikVorm 在 3 个编辑器模板中的 2 个中为空。

4

1 回答 1

0

将所有视图包放入ActionResult方法中,如下所示。

public ActionResult SetNewInputForm(InitializeNewInputViewModel model)
    {
       var Input = new Input(model.AantalPrimairen,model.AantalSecundairen);
        ViewBag.Blikvorm = Enum.GetNames(typeof(Common.BlikVorm)).ToList();
        ViewBag.Kwaliteit = Database.Kwaliteit.ToList();
        ViewBag.BlikFabrikanten = Database.BlikFabrikant.ToList();
        ViewBag.Walserijen = Database.Walserij.ToList();
        ViewBag.BlikMontage = new SelectList(Database.KernMontage.ToList(),  "Id", "KernMontageNaam");
        ViewBag.SchakelWijze = Enum.GetNames(typeof(Common.SchakelWijze)).ToList();        
        ViewBag.AantalGaten = Enum.GetNames(typeof(Common.AantalGaten)).ToList();
        ViewBag.DiameterGaten = Database.DiameterGat.ToList();
        ViewBag.BlikFormaat = Database.BlikFormaat.ToList();
        ViewBag.KernMontage = Database.KernMontage.ToList();
        ViewBag.KokerFabrikanten = Database.KokerFabrikant.ToList();
        return View(Input);
    }
于 2017-01-09T07:06:44.833 回答