在editorstemplate MVC中,Viewdata / Viewbag为空

时间:2017-01-06 12:29:01

标签: asp.net-mvc viewbag editortemplates

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

在视图中,我使用editortemplates显示模型中的复杂对象。

问题是editortemplates中有几个viewbags为空。

如果我让调试器停在控制器中,我的视图包装都正确。

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

wath会导致这个问题吗?

的Controler:

    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个editorTemplates中的2个中是空的。

1 个答案:

答案 0 :(得分:0)

将所有viewbags放在 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);
    }