how to fix play controller error : incompatible types: long cannot be converted to List<general_store>

时间:2016-04-15 15:05:29

标签: playframework playframework-2.2

I was wanting to fetch the sum of table column data "amount" and return its sum as total. I appreciate your help

models

Controllers method

Template code:

@(tasks: List[General_store], taskForm: Form[General_store])
@main("current balance") {


<div class="col-md-5  col-md-offset-3   client-margin">
    <div class="panel panel-success">
        <div class="panel-heading">
          current Balance
        </div>
        <div class="panel-body"> <br/><br/><br/>
            @for(storedb <- tasks) {



                <p>
                    <b >Balance:</b>@storedb.amount
                </p>


            }

        </div>

    </div>
</div>
}

1 个答案:

答案 0 :(得分:0)

问题与模板的参数有关。您已指定了两个参数 - 商店列表和表单

@(tasks: List[General_store], taskForm: Form[General_store])

但是从您的控制器中您实际提供了一个长(General_store.sumOfStores())和一个表单(taskData)。您会看到参数数量匹配但不匹配参数类型

解决方案:您必须更改控制器代码:

return ok(views.html.balance.render(General_store.sumOfStores(), taskData)

为:

return ok(views.html.balance.render(General_store.sumOfStores(), General_store.all(), taskData)

中的模板参数定义

@(tasks: List[General_store], taskForm: Form[General_store])

为:

@(totalSum: Long, tasks: List[General_store], taskForm: Form[General_store])

如果您想在页面上显示总和,则必须将此行添加到模板中(例如在<div class="panel-body">之后:

<h2>Total sum is: @totalSum</h2>