【问题标题】:How to pass data from one partial view to another partial View?如何将数据从一个局部视图传递到另一个局部视图?
【发布时间】:2014-02-01 04:40:36
【问题描述】:

我正在用 MVC 开发一个应用程序。

我有一个视图,其中绑定了两个部分视图。

我想做的是,我想将数据从一个局部视图传递到另一个局部视图。

在下面的示例中,产品列表部分视图给出了产品列表的总价格,我想将该总价格传递给税收部分视图以计算总价值的税金。

它应该在运行时完成。

如何做到这一点?

我的父视图

    @model CRMEntities.Quotation
    @{
    }

     <span class="ProductForm row-fluid">
     @Html.Action("QuotationRelatedProductList", "Quotation", new { Id = @Model.Id })
     </span>
Product Partial View
    @model IEnumerable<CRMEntities.QuotationProduct>

    <div id="divOpportunityProduct">
        @if (Model != null)
        {
            i = 0;
            foreach (var item in Model)
            {
 <input class="clsPrice" name='ProductPrice' type='text' id='txtPrice-@i' value="@EachProdPrice" style='font-size:12px;width:50px;margin-left:12px;' @*readonly="true"*@/>

<input name="Total" class="clsTotal" type='text' id='lblTotal-@i' style ='font-size:12px;width:50px;margin-left:14px;' @*readonly="true"*@/>
            }
        }        

<div class="pull-left row-fluid" style="margin-top: 5px;">@Html.TextBox("txtGrandTotal", null, new {style="width:110px;"})</div>


<script type="text/javascript">

  function GetGrandTotal() {
        var AddTotal = 0;
        var EachTotal;
        $('input.clsTotal').each(function (index) {

            var d = "lblTotal-" + index;

            EachTotal = $('#' + d).val();

            var y = new Number(EachTotal);

            AddTotal = (AddTotal + y);

        });

        $("#txtGrandTotal").val(AddTotal);

    }

</script>

现在,上面的总价值,我想在运行时传递给另一个视图....

【问题讨论】:

  • 您最好花时间编写代码来描述您已经尝试过的内容,而不是制作图形。
  • 好的,但是代码太庞大,放在这里...
  • 请检查代码...

标签: asp.net-mvc model-view-controller


【解决方案1】:

A.在服务器(在控制器中)计算业务逻辑中的总数。将它与您的视图模型一起传递。将其赋予您的两个局部视图。

B.部分在服务器上呈现。 Javascript 仅适用于完整页面。 JS 没有部分内容。因此,如果您在一个视图中执行一些 JS,它也可用于另一个视图 - 将结果保存到全局变量中并从脚本的其他部分访问它。但这是一种糟糕的技术。

只需在浏览器中查看生成的页面,看看 JS 是如何工作的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多