0

I'm trying to render two tables via one model. I'm using my helper dll to create the tables. the problem is, I don't know how I can render only parts of my viewdata, at the first call of the page. here is some code:

Controller: (Creating two tables and the viewdata, returning the view, with model viewdata)

public class Controller1 : Controller {      
//some code
public ActionResult Steuertabellen()
    {
    table = table_fill();
    table= Tabelle.tabelleGenerieren(
    table,
    new Dictionary<string, SortierRichtung>(),
    new List<string>(),
    new List<string>()               
    );


    table2 = table_fill();
    table2 = Tabelle.tabelleGenerieren(
    table2,
    new Dictionary<string, SortierRichtung>(),
    new List<string>()               
    );


    VD_Planung_Prognosen viewdata = new VD_Planung_Prognosen();
    viewdata.table= table;
    viewdata.table2= table2;

    return view(viewdata);
}

View:

@model namespace.Models.VD_Planung_Prognosen
@using namespace.Models;

<div class="tabelle_partialview" id="tabelle_partialview_@(tabellen2ID)">@{Html.RenderPartial("PV_table2", Model);}</div>
<div id="optionen_tabelle">
    <div id="optionen_rechts">
        <button class="btn_speichern btn_speichern_@(tabellenID)" id="btn_speichern">Speichern</button>
        <button class="btn_abbrechen btn_abbrechen_@(tabellenID)" id="btn_abbrechen_@(tabellenID)">Abbrechen</button>
    </div>
</div>
<div class="tabelle_partialview" id="tabelle_partialview_@(tabellenID)">@{Html.RenderPartial("PV_table", Model);}

PartialViews (edited):

@model namespace.Models.VD_Planung_Prognosen
@using HelperLib_Html.Tabelle.v1_02;

@{
@Html.createTabelle(Model.tabel1)
}
//<some style>

Edit I added the model (sry i should have done that before probably)

Model:

public class VD_Planung_Prognosen
  {
    public Matrix matrix  { get; set; }
    public Tabelle table1{ get; set; }
    public Tabelle table2{ get; set; }
    public List<stored_procedure1> years{ get; set;     }
    public List<stored_procedure2> groups{ get; set; }
    public List<stored_procedure3> cost{ get; set; }
    public List<stored_procedure4> costs2{ get; set; }
    public List<stored_procedure5> data{ get; set; }
    public int year{ get; set; }
    public string grp{ get; set; }
    public bool pflegeSteuertabellen { get; set; }
}

So this wont work, because the data presented in the actionlink above shows in one model, the two tables will follow each other, while the renderpartial shows one, then the buttons then the second. How can I use the model so that I can address only one table?

I hope my question is understandable, it seems quite comlicated to explain ;)

Thanks in advance

Daniel

Edit1: Of course the PV are created in the controller too, quite similar to the ActionLink view

tereško
  • 58,060
  • 25
  • 98
  • 150
Daniel
  • 31
  • 5

1 Answers1

1

I am not sure to understand what your problem is, but did you try to change your RenderPartial calls to

@{Html.RenderPartial("PV_table2", Model.table2);}

and

@{Html.RenderPartial("PV_table", Model.table);}

When you render partial views, you provide the view with an instance of its own model type specified in the partial view at its top :

@model SomeNamespace.SomeModelType

In the view you show here, the variable Model refers to your controller variable "viewdata", whereas your partials should expect variables that have the same type as your members viewdata.table and viewdata.table2.

Side note : you may not name your model "viewdata", it is a bit confusing because ViewData is the main data dictionary for your view and it actually contains the model you pass.

Edit : This should work, here is what you have to do. Your partial views must accept the table type (let's name it TableModel) as their own model type, not VD_Planung_Prognosen.

  • In your partials, change @model namespace.Models.VD_Planung_Prognosen to @model namespace.Models.TableModel

  • In your partials, "Model" word will refer to the partial's model, not the model of your main view. So your views should look like :

    @model namespace.Models.TableModel

    @using HelperLib_Html.Tabelle.v1_02;

    @{ @Html.createTabelle(Model) } //

  • Finally in your main view, change your renderpartials to

    @{Html.RenderPartial("PV_table", Model.table);}

and

@{Html.RenderPartial("PV_table2", Model.table2);}
Sbu
  • 910
  • 11
  • 22
  • I can't change the RenderPartial calls like that, because the dictionary requires the above given model "VD_Planung_Prognosen", not the table :( – Daniel Jun 11 '13 at 09:01
  • If you can change your partial views PV_table and PV_Table2, then you can make them accept your table members as their model type. Or you can make them display data from each table member of this parent model, i.e. table and table2. You did not show these partials code btw. – Sbu Jun 11 '13 at 09:21
  • Added the PartialView for one table, the other one is similar, the table members are just part of a model, not a model itself, so it won't work I think?! How could I make them display data form a member of the model? Thats what I'm trying, but it's not working for now...:/ – Daniel Jun 11 '13 at 09:28
  • Thank you for your effort! I can't change the tables to models, it might work like that, but the model contains more data that I need to pass. With the tables as seperated models I would have another problem, because how could I pass them as a single view to the return of ActionResult. – Daniel Jun 11 '13 at 10:17