在Razor中有效显示查询结果(Display query results efficiently in Razor)

我用Visual C#和“Razor”选项创建了一个ASP.NET MVC 4 Web应用程序

HomeController.cs

    public ActionResult Tasks()
    {
        ViewBag.Message = "Tasks";

        string selectSql = "select * from Tasks";

        string connectionString = @"Data Source=lpc193\adamssqlserver;Database=master;Integrated Security=True;";

        DateTime strt;

        using (var cn = new SqlConnection(connectionString))
        using (var cmd = new SqlCommand(selectSql, cn))
        {
            cn.Open();

            using (var reader = cmd.ExecuteReader())
            {
                if (reader.Read())
                {
                    strt = reader.GetDateTime(reader.GetOrdinal("Start"));
                }
            }
        }

        return View();
    }

Tasks.cshtml

@{ 
    ViewBag.Title = "Tasks To Complete"; 
}

These are the tasks from the table

<!-- some datagrid object here ?? -->

我有以下SQL Server 2012数据库表布局

在此处输入图像描述

我能够很好地查询表格并单独提取每个字段的值。

我想要做的是将DataGridView中的结果集(包含select *查询中的所有字段)显示给用户。 我可以很容易地将每个值放入一个TR TD并将其放入一个字符串中,将它显示在.cshtml一侧......但那并没有真正教会我什么,我怀疑它非常有效。

注意事项找到有关Razor的相关信息并显示查询数据非常困难。 是因为它是新的,和/或是Razor要避免的C#新手?


I've created an ASP.NET MVC 4 Web Application with Visual C# and with the "Razor" option

HomeController.cs

    public ActionResult Tasks()
    {
        ViewBag.Message = "Tasks";

        string selectSql = "select * from Tasks";

        string connectionString = @"Data Source=lpc193\adamssqlserver;Database=master;Integrated Security=True;";

        DateTime strt;

        using (var cn = new SqlConnection(connectionString))
        using (var cmd = new SqlCommand(selectSql, cn))
        {
            cn.Open();

            using (var reader = cmd.ExecuteReader())
            {
                if (reader.Read())
                {
                    strt = reader.GetDateTime(reader.GetOrdinal("Start"));
                }
            }
        }

        return View();
    }

Tasks.cshtml

@{ 
    ViewBag.Title = "Tasks To Complete"; 
}

These are the tasks from the table

<!-- some datagrid object here ?? -->

I have the following SQL Server 2012 database table layout

enter image description here

I'm able to query the table just fine and pull out each field's value individually.

What I want to do is display the result set (with all fields from the select * query) in a DataGridView of sorts to the user. I could easily place each value into a TR TD and plop that into a string, display it on the .cshtml side... but thats doesn't really teach me anything, and I doubt its very efficient.

side note It's extremely difficult to find relevant information regarding Razor and displaying data from a query. Is it because its new, and/or is Razor something to avoid for a C# novice?


原文:https://stackoverflow.com/questions/16593359
2021-10-08 16:10

满意答案

希望这有帮助。

var app = angular.module('webStatistics', ['ngRoute']);
app.config(function($routeProvider){
    $routeProvider
        .when('/stats/views/', {templateUrl: 'home'})
        .when('/stats/views/home.html', {
            controller : 'testController'
            templateUrl: '/stats/views/home.html'
        })
        .otherwise({redirectTo : '/'});
});
app.controller('testController', function($scope, $http) {
    $scope.item1 = '';
    $scope.item2 = '';

    $scope.getData() {

        if ($scope.item1 && $scope.item2) {
            var api = '../app/sources/stats/004/' + item1 + '_' + item2 + '.json';
            $http.get(api).success(function(data) {
                $scope.variables = data;
            });
        }

    };
});

Hope this help.

var app = angular.module('webStatistics', ['ngRoute']);
app.config(function($routeProvider){
    $routeProvider
        .when('/stats/views/', {templateUrl: 'home'})
        .when('/stats/views/home.html', {
            controller : 'testController'
            templateUrl: '/stats/views/home.html'
        })
        .otherwise({redirectTo : '/'});
});
app.controller('testController', function($scope, $http) {
    $scope.item1 = '';
    $scope.item2 = '';

    $scope.getData() {

        if ($scope.item1 && $scope.item2) {
            var api = '../app/sources/stats/004/' + item1 + '_' + item2 + '.json';
            $http.get(api).success(function(data) {
                $scope.variables = data;
            });
        }

    };
});

相关问答

更多

如何在Javascript / Angularjs中检测用户来自哪里的URL?(How do I detect the url of where the user is coming from in Javascript/Angularjs?)

你在寻找推荐人吗? var x = document.referrer; Are you looking for referrer? var x = document.referrer;

Angularjs应用程序没有将个人用户的ID传递给url(Angularjs app not passing id of individual user to url)

您实际上可以使用一个工厂作为资源 app.factory('UserFactory', function ($resource) { return $resource('https://example.com/users/:id', null, { update: { method: 'PATCH' } }) }); AngularJS $资源 在您的控制器中,您使用$scope.user = UserFactory.get({id: $routeParams.user...

根据条件授予用户访问URL的权限(Angularjs)(Give user access to URL based on condition (Angularjs))

当用户自己键入URL时,这是不可能实现的; 您的$ scope更改,不再可访问。 解决这个问题的一种方法是在$ window变量中设置值; 这可以从您为Pre.html定义的控制器中进行ping操作。 例如: var controllers = angular.module('controllers', []); controllers.controller('PageCtrl', [ '$scope', '$window', $location, function($scope, $window...

AngularJs $资源动态URL(AngularJs $resource dynamic URL)

您描述的第一个URL遵循RESTful约定,但不是第二个。 当URI中有动作动词(此处为displayName)时,您的API不再是RESTful。 AngularJS $资源仅与RESTful服务器端数据源交互。 您的API必须遵循许多RESTful约定。 我建议你阅读这个高度相关的问题的答案并描述这些约定。 由于您的API不符合这些条件,您必须基于较低级别的$ http服务构建自己的类资源CRUD 服务 。 ( 这里的例子 ) The first URL you describe follow...

获取用户来自AngularJS的url(Get the url that a user is coming from AngularJS)

您可以在我们的JavaScript中使用浏览器历史记录,或者您可以在Cookie中编写最后一页,并获取最后一个链接,然后进行更新 You can use browser history in our javascript or you can write your last page in cookies and get the last link then update it

在angularjs中的引荐网址(Referrer url in angularjs)

您只能使用$ rootScope或定义服务来保存旧位置 You can only use $rootScope or define a service to save the old location

根据用户选择AngularJS构造URL(Construct URL depending on user choice AngularJS)

希望这有帮助。 var app = angular.module('webStatistics', ['ngRoute']); app.config(function($routeProvider){ $routeProvider .when('/stats/views/', {templateUrl: 'home'}) .when('/stats/views/home.html', { controller : 'testContr...

AngularJS更新表单有选择菜单选项吗?(AngularJS update form with select menu choice?)

<!DOCTYPE html> <html ng-app="plunker"> <head> <meta charset="utf-8" /> <title>AngularJS Plunker</title> <link data-require="bootstrap@3.3.7" data-semver="3.3.7" rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstr...

Angularjs - 在不更改URL的情况下更改模板(Angularjs - change templates without changing url)

我建议使用ng-include。 这允许您从其他位置包含html代码块。 然后,您可以使用ng-show / hide显示所需的片段或ng - 如果您希望它不在dom中,如果不需要 I'd suggest using ng-include. This allows you to include chunks of html code from another location. You could then use ng-show/hide to display the desired piece...

根据选择跳过问题(Skips questions depending on a choice)

根据“ContentClassification”的选择,跳过其他问题。 您可以使用FieldReflector实现自己的IField ,例如: public enum ContentClassification { Confidential_Restricted = 1, Confidential_Secret = 2, Public = 3, Strictly_Confidential = 4, help = 5 }; public enum Stat...

相关文章

更多

MongoDB学习 (五):查询操作符(Query Operators).1st

查询操作符(Query Operators)可以让我们写出复杂查询条件,让我们使用的过程更加灵活。官方 ...

常用HQL(Hibernate Query Language)查询

查询一个对象(实体类必须有一个不带参数的构造方法),使用select查询,基于投影的查询,通过在列表中 ...

Solr 使用自定义 Query Parser

原文出处:http://blog.chenlb.com/2010/08/solr-use-custom ...

Solr 使用自定义 Query Parser

原文出处:http://blog.chenlb.com/2010/08/solr-use-custom ...

solr学习笔记二-------solr query查询的参数

初步接触solr是对其query的各个参数都不是很了解,现在做一个总结,以便日后查看使用 各个参数及意 ...

solr delete query

Solr1.4 Both delete by id and delete by query can b ...

solr delete query

Solr1.4 Both delete by id and delete by query can b ...

Mysql错误:Ignoring query to other database解决方法

用source c:\xxx.sql,出现Ignoring query to other databa ...

solr 分页查询与高亮显示

1、分页查询 分页查询的时候,只对添加start和rows两个参数即可 @Testpublic voi ...

Solr的一些查询参数

fl: 是逗号分隔的列表,用来指定文档结果中应返回的Field集。默认为 “*”,指所有的字段。 d ...

最新问答

更多

获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)

我用Google搜索了一个解决方案。 “EnumDisplayModeProvider”是我自己设置网站的各种模式的枚举。 public EnumDisplayModeProvider GetDisplayModeId() { foreach (var mode in DisplayModeProvider.Instance.Modes) if (mode.CanHandleContext(HttpContext)) {

如何通过引用返回对象?(How is returning an object by reference possible?)

这相对简单:在类的构造函数中,您可以分配内存,例如使用new 。 如果你制作一个对象的副本,你不是每次都分配新的内存,而是只复制指向原始内存块的指针,同时递增一个也存储在内存中的引用计数器,使得每个副本都是对象可以访问它。 如果引用计数降至零,则销毁对象将减少引用计数并仅释放分配的内存。 您只需要一个自定义复制构造函数和赋值运算符。 这基本上是共享指针的工作方式。 This is relatively easy: In the class' constructor, you allocate m

矩阵如何存储在内存中?(How are matrices stored in memory?)

正如它在“熵编码”中所说的那样,使用Z字形图案,与RLE一起使用,在许多情况下,RLE已经减小了尺寸。 但是,据我所知,DCT本身并没有给出稀疏矩阵。 但它通常会增强矩阵的熵。 这是compressen变得有损的点:输入矩阵用DCT传输,然后量化量化然后使用霍夫曼编码。 As it says in "Entropy coding" a zig-zag pattern is used, together with RLE which will already reduce size for man

每个请求的Java新会话?(Java New Session For Each Request?)

你是如何进行重定向的? 您是否事先调用了HttpServletResponse.encodeRedirectURL()? 在这里阅读javadoc 您可以使用它像response.sendRedirect(response.encodeRedirectURL(path)); The issue was with the path in the JSESSIONID cookie. I still can't figure out why it was being set to the tomca

css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)

我认为word-break ,如果你想在一个单词中打破行,你可以指定它,这样做可以解决问题: .column { word-break:break-all; } jsFiddle演示。 您可以在此处阅读有关word-break属性的更多信息。 I think word-break, with which you can specify if you want to break line within a word, will do the trick: .column { word-break

无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)

我认为您忘记在分类时间内缩放输入图像,如train_test.prototxt文件的第11行所示。 您可能应该在C ++代码中的某个位置乘以该因子,或者使用Caffe图层来缩放输入(请查看ELTWISE或POWER图层)。 编辑: 在评论中进行了一次对话之后,结果发现在classification.cpp文件中错误地删除了图像均值,而在原始训练/测试管道中没有减去图像均值。 I think you have forgotten to scale the input image during cl

xcode语法颜色编码解释?(xcode syntax color coding explained?)

转到: Xcode => Preferences => Fonts & Colors 您将看到每个语法高亮颜色旁边都有一个简短的解释。 Go to: Xcode => Preferences => Fonts & Colors You'll see that each syntax highlighting colour has a brief explanation next to it.

在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)

你考虑过第三方拼写检查吗? 您可以将在C#中开发的自定义WinForms控件插入访问数据库吗? VB6控件怎么样? 如果你能找到一个使用第三方库进行拼写检查的控件,那可能会有效。 Have you considered a third party spell checker? Can you insert a custom WinForms controls developed in C# into an access database? What about a VB6 control? If

从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)

我有同样的问题,因为我在远程服务器上有两个图像,我需要在每天的预定义时间复制到我的本地服务器,这是我能够提出的代码... try { if(@copy('url/to/source/image.ext', 'local/absolute/path/on/server/' . date("d-m-Y") . ".gif")) { } else { $errors = error_get_last(); throw new Exception($err

从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))

我不确定我完全明白你在说什么。 你能编辑你的帖子并包含你正在做的Subversion命令/操作的特定顺序吗? 最好使用命令行svn客户端,以便容易为其他人重现问题。 如果您只是想获取文件的旧副本(即使该文件不再存在),您可以使用如下命令: svn copy ${repo}/trunk/moduleA/file1@${rev} ${repo}/trunk/moduleB/file1 其中${repo}是您的存储库的URL, ${rev}是您想要的文件的版本。 这将恢复该文件的旧版本,包括最高版本