Viewdata / Viewbag is empty in editorstemplate MVC

468 views Asked by At

Mvc application with simple controller where i return a view with a model. into u create view. Couse of the large amount of dropdownlists i have several viewbags(13).

In the view I work with editortemplates to display the complex objects from my model.

The problem is that there are several viewbags empty in the editortemplates.

If I let the debugger stop in the controller my viewbags are all filled correct.

If I let the debugger stop in the view I can see that some of them are empty.

wath can cause this problem?

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();
    }

part of my view

@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>

part of my editor template:

@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 is empty all the time Viewbag.BlikVorm is empty in 2 of the 3 editorTemplates.

1

There are 1 answers

1
Curiousdev On

Put your all viewbags in ActionResult method as below.

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);
    }