简体   繁体   English

ASP.NET MVC:如何在Controller加载时将Controller返回的变量打印到View?

[英]ASP.NET MVC: How to print a variable returned from a Controller to the View on page load?

I want to sum all the values in a column of my table in SQL Database and display it on page load itself. 我想在SQL数据库的表的一列中汇总所有值,并在页面加载本身上显示它。 What I did is: 我做的是:

public ActionResult Sumthem() {
    string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    SqlConnection con = new SqlConnection(constr);
    SqlCommand cmd = new SqlCommand("SELECT SUM (Final) FROM SomeTable", con);
    cmd.Connection.Open();    


    ViewBag.FinalSUM = cmd.ExecuteScalar().ToString();

    return View("Records");   
}     

I want to print it like this (in Records.cshtml view): 我想像这样打印它(在Records.cshtml视图中):

@{var sum = ViewBag.FinalSUM;}
<h2>The Total is: @sum</h2>

Where and how should I call the controller method? 我应该在哪里以及如何调用控制器方法? I tried @Url.Action("Sumthem", "Records") in the Razor, but the controller method was not getting called. 我在Razor中尝试了@Url.Action("Sumthem", "Records") ,但控制器方法没有被调用。 How can I return and print the value of the sum as soon as the page loads? 如何在页面加载后立即返回并打印总和的值?

Edit: I tried this after that, but there was a System.StackOverflow error 编辑:之后我尝试了这个,但是有一个System.StackOverflow错误

@{Html.RenderAction("Sumthem", "Records");
var sum = ViewBag.FinalSUM;}
<h2>The Total is: @sum</h2>
//Returned PartialView instead of View from the Controller.

Action : 行动

public ActionResult Sumthem() {
    string constr = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        SqlCommand cmd = new SqlCommand("SELECT SUM (Final) FROM SomeTable", con);
        cmd.Connection.Open();

        ViewBag.FinalSUM = cmd.ExecuteScalar().ToString();

        return PartialView("Records", new YourModel())
    }   
}    

Partial View (Records.chstml): 部分视图 (Records.chstml):

@model YourModel
@*You can use data from your model as well*@
<h2>The Total is: @ViewBag.FinalSUM</h2>

Usage (in any view where you actually need your total value to be displayed): 用法 (在您实际需要显示总值的任何视图中):

@Html.RenderAction("Sumthem", "Records")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET MVC-如何在Webapi控制器返回的Razor视图中显示数据 - ASP.NET MVC - How to display data in Razor view returned from Webapi controller ASP.NET MVC 将值从控制器上的变量传递到视图 - ASP.NET MVC passing a value from a variable on a controller to a view ASP.NET - MVC 4使用从控制器到视图的变量 - ASP.NET - MVC 4 using a variable from a controller to a view 如何从控制器加载大型数据集到asp.net mvc中查看 - How to load large data set from controller to view in asp.net mvc 如何从控制器 ASP.NET MVC 的视图页面中自动添加所选 id 的名称 - How to automatically add a name from selected id in View page from controller ASP.NET MVC 如何将数据从控制器传递到 ASP.NET MVC 中查看 - How to to pass data from controller to view in ASP.NET MVC 将变量从视图传递到 controller 操作方法,然后传递到同一 ASP.NET MVC controller 中的另一个方法 - Pass a variable from a view to a controller action method and then to another method in the same ASP.NET MVC controller 如何在ASP.NET MVC 4 Web应用程序中从一个控制器的视图重定向到另一个控制器的视图 - How to redirect to a View of another Controller from one Controller's View in ASP.NET MVC 4 Web Application 从搜索者(从视图)发送短语(变量)到控制器,asp.net mvc - Sending a phrase (variable) from searcher (from view) to controller, asp.net mvc 从ASP.NET MVC中的AngularJS控制器加载数据而无需加载整个页面 - Load data from AngularJS controller in ASP.NET MVC without loading entire page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM