Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
50 views
in Technique[技术] by (71.8m points)

How can I call a controller method in a view using ASP.NET CORE

I have a controller with a method that returns a value:

public float GetShoppingCartTotal()
    {
        string user = User.Identity.Name;

        var total = _context.ShutterOrders.Where(c => c.User == user).Select(c => c.Price * c.Amount).Sum();

        return total;
    }

How can I call this method from a view so I can show its value, here is my view (P.S. I took out some code of the view since I think it really is unnecessary for the purpose of this question. I have marked the part where I would like to call my controller function):

@model IEnumerable<ShuttersInc.Areas.Identity.Data.ShutterOrders>
<tfoot>
    <tr>
        <td colspan="3" class="text-right">Total:</td>
        <td class="text-right">
         **I'd like to place it here**
        </td>
    </tr>
</tfoot>

P.P.S - I have an IEnumerable model since I list items from a list. I think this is one of the main problems why I can't call the method from the controller, although I might be mistaken.

question from:https://stackoverflow.com/questions/65874045/how-can-i-call-a-controller-method-in-a-view-using-asp-net-core

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...