【问题标题】:Persisting session data to child views MVC 3将会话数据持久化到子视图 MVC 3
【发布时间】:2012-07-23 17:54:50
【问题描述】:

所以我正在开发一个 MVC 3 项目,该项目从旧数据源的多个 (10) 表中提取到具有 6 个部分的主视图。有一个表包含每个子视图的数据,因此我们决定将其存储在会话数据中,然后使用所需的任何其他数据填充其余子视图。

当我们最初尝试这样做时,我们得到了会话数据的空引用异常。我想出了一个解决方案,但它看起来很笨拙,我认为这不是最佳实践/引入不必要的状态。

相关代码如下:

这是我们在主控制器上的:

public ActionResult PolicyView(string PolicyID)
    {
        IPolicyHolder phdata = new PolicyHolderData();
        Polmast policy = phdata.GetPolicyFromUV(PolicyID);
        ViewBag.FullName = policy.FULLNAME;
        ViewBag.PolicyID = PolicyID;
        Session["polmast"] = policy;
        return View("PolicyView");
    }

然后在我们的主视图中,部分子视图的链接之一:

<div id="Billing">
@{ Html.RenderAction("Billing", Session["polmast"] ); }
</div>

在子控制器中:

public ActionResult Billing(object sessiondata)
    {
        return PartialView("_Billing", sessiondata);
    }

在子视图中:

@{var polmast = (Polmast)Session["polmast"];}
**snip**

<table id="premiumsgrid" class="display" border="1" 
cellpadding="0" cellspacing="0" width="50%">
<thead>
    <tr>
        <th>Annual</th>
        <th>Semi-Annual</th>
        <th>Quarterly</th>
        <th>Monthly</th>
    </tr>
</thead>
<tbody>
    <tr>
        <td>@polmast.PAN</td>
        <td>@polmast.PSA</td>
        <td>@polmast.PQT</td>
        <td>@polmast.PMO</td>
    </tr>

</tbody>
</table>

【问题讨论】:

    标签: asp.net asp.net-mvc-3 session razor


    【解决方案1】:

    我建议开始使用模型并将它们返回到您的视图中,而不是传递会话对象并将其投射到您的视图中。它会使这段代码更干净。

    这就是我构建代码的方式:

    public ActionResult PolicyView(string PolicyID)
        {
            IPolicyHolder phdata = new PolicyHolderData();
            Polmast policy = phdata.GetPolicyFromUV(PolicyID);
    
            PolicyModel model = new PoliceModel() {
                FullName = policy.FULLNAME,
                PolicyID = PolicyID
                //Populate other properties here.
            };
    
            Session["polmast"] = policy;
    
            return View("PolicyView", model);
        }
    

    然后我会设置你的主视图(不需要用花括号把这个调用包裹起来,你也不应该传递任何路由值):

    <div id="Billing">
        @Html.RenderAction("Billing")
    </div>
    

    子控制器:

    public ActionResult Billing()
        {
            //Get the data out of session; it should already exist since your parent controller took care of it.
            var policyData = (Polmast)Session["polmast"];
    
            PolicyModel model = new PoliceModel() {
                FullName = policy.FULLNAME,
                PolicyID = PolicyID
                //Populate other properties here.
            };
    
            return PartialView("_Billing", model);
        }
    

    你的孩子的看法:

    @model 波尔马斯特 剪辑

    <table id="premiumsgrid" class="display" border="1" 
    cellpadding="0" cellspacing="0" width="50%">
    <thead>
        <tr>
            <th>Annual</th>
            <th>Semi-Annual</th>
            <th>Quarterly</th>
            <th>Monthly</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>@Model.PAN</td>
            <td>@Model.PSA</td>
            <td>@Model.PQT</td>
            <td>@Model.PMO</td>
        </tr>
    
    </tbody>
    </table>
    

    【讨论】:

    • 是的,是的。请将您的视图绑定到模型。这就是 MVC 应该做的。我也会避免在会话中存储实际对象。如果您可以将其转换为字符串,或者仅存储一些 int 标识所涉及的实体,这样可以更清晰地工作。
    • 我采用的解决方案是沿着这些思路。我为传递会话数据的孩子创建了 viewModel,插入特定于视图的数据,然后将其传递。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-21
    • 1970-01-01
    • 2019-03-14
    • 1970-01-01
    • 2013-04-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多