【问题标题】:mvc+razor trying to render just parts of the modelmvc+razor 试图只渲染模型的一部分
【发布时间】:2013-06-11 08:11:03
【问题描述】:

我正在尝试通过一个模型呈现两个表格。我正在使用我的帮助程序 dll 来创建表。 问题是,我不知道如何在第一次调用页面时只渲染部分视图数据。 这是一些代码:

控制器:(创建两个表和视图数据,返回视图,带有模型视图数据)

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

查看:

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

部分视图(已编辑):

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

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

编辑 我添加了模型(抱歉,我之前可能应该这样做)

型号:

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

所以这是行不通的,因为上面的 actionlink 中显示的数据显示在一个模型中,两个表将相互跟随,而 renderpartial 显示一个,然后是按钮,然后是第二个。如何使用该模型以便只能处理一张表?

我希望我的问题可以理解,解释起来似乎很复杂;)

提前致谢

丹尼尔

Edit1:当然 PV 也是在控制器中创建的,非常类似于 ActionLink 视图

【问题讨论】:

    标签: asp.net-mvc razor model viewdata


    【解决方案1】:

    我不确定您的问题是什么,但您是否尝试将您的 RenderPartial 调用更改为

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

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

    当你渲染局部视图时,你为视图提供了一个在其顶部的局部视图中指定的模型类型的实例:

    @model SomeNamespace.SomeModelType
    

    在您在此处显示的视图中,变量 Model 引用您的控制器变量“viewdata”,而您的部分变量应该期望与您的成员 viewdata.table 和 viewdata.table2 具有相同类型的变量。

    旁注:您不能将模型命名为“viewdata”,这有点令人困惑,因为 ViewData 是您的视图的主要数据字典,它实际上包含您传递的模型。

    编辑: 这应该工作,这是你必须做的。您的局部视图必须接受表类型(我们将其命名为 TableModel)作为它们自己的模型类型,而不是 VD_Planung_Prognosen。

    • 在您的部分中,将@model namespace.Models.VD_Planung_Prognosen 更改为@model namespace.Models.TableModel

    • 在您的局部视图中,“模型”一词指的是局部模型,而不是主视图的模型。所以你的观点应该是这样的:

      @model 命名空间.Models.TableModel

      @使用 HelperLib_Html.Tabelle.v1_02;

      @{ @Html.createTabelle(模型) } //

    • 最后在您的主视图中,将您的渲染部分更改为

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

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

    【讨论】:

    • 我不能像那样更改 RenderPartial 调用,因为字典需要上面给定的模型“VD_Planung_Prognosen”,而不是表格:(
    • 如果你可以改变你的局部视图 PV_table 和 PV_Table2,那么你可以让他们接受你的表成员作为他们的模型类型。或者你可以让它们显示来自这个父模型的每个表成员的数据,即 table 和 table2。顺便说一句,您没有显示这些部分代码。
    • 为一个表添加了PartialView,另一个类似,表成员只是模型的一部分,而不是模型本身,所以我认为它不起作用?!我怎样才能让他们显示来自模型成员的数据?这就是我正在尝试的,但它现在不起作用......:/
    • 感谢您的努力!我无法将表格更改为模型,它可能会像那样工作,但模型包含更多我需要传递的数据。将表格作为单独的模型我会遇到另一个问题,因为我如何将它们作为单个视图传递给 ActionResult 的返回。
    猜你喜欢
    • 2015-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多