在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
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
最满意答案
希望这有帮助。
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; }); } }; });
相关问答
更多-
你在寻找推荐人吗? var x = document.referrer; Are you looking for referrer? var x = document.referrer;
-
您实际上可以使用一个工厂作为资源 app.factory('UserFactory', function ($resource) { return $resource('https://example.com/users/:id', null, { update: { method: 'PATCH' } }) }); AngularJS $资源 在您的控制器中,您使用$scope.user = UserFactory.get({id: $routeParams.userId}); ...
-
当用户自己键入URL时,这是不可能实现的; 您的$ scope更改,不再可访问。 解决这个问题的一种方法是在$ window变量中设置值; 这可以从您为Pre.html定义的控制器中进行ping操作。 例如: var controllers = angular.module('controllers', []); controllers.controller('PageCtrl', [ '$scope', '$window', $location, function($scope, $window, $lo ...
-
您描述的第一个URL遵循RESTful约定,但不是第二个。 当URI中有动作动词(此处为displayName)时,您的API不再是RESTful。 AngularJS $资源仅与RESTful服务器端数据源交互。 您的API必须遵循许多RESTful约定。 我建议你阅读这个高度相关的问题的答案并描述这些约定。 由于您的API不符合这些条件,您必须基于较低级别的$ http服务构建自己的类资源CRUD 服务 。 ( 这里的例子 ) The first URL you describe follows RES ...
-
您可以在我们的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)[2023-07-21]
您只能使用$ rootScope或定义服务来保存旧位置 You can only use $rootScope or define a service to save the old location -
希望这有帮助。 var app = angular.module('webStatistics', ['ngRoute']); app.config(function($routeProvider){ $routeProvider .when('/stats/views/', {templateUrl: 'home'}) .when('/stats/views/home.html', { controller : 'testController ...
-
AngularJS Plunker 我建议使用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 or n ...根据选择跳过问题(Skips questions depending on a choice)[2021-10-29]
根据“ContentClassification”的选择,跳过其他问题。 您可以使用FieldReflector实现自己的IField ,例如: public enum ContentClassification { Confidential_Restricted = 1, Confidential_Secret = 2, Public = 3, Strictly_Confidential = 4, help = 5 }; public enum StatusCon ...相关文章
更多- MongoDB学习 (五):查询操作符(Query Operators).1st
- 常用HQL(Hibernate Query Language)查询
- Solr 使用自定义 Query Parser
- Solr 使用自定义 Query Parser
- solr学习笔记二-------solr query查询的参数
- solr delete query
- solr delete query
- Mysql错误:Ignoring query to other database解决方法
- solr 分页查询与高亮显示
- Solr的一些查询参数
最新问答
更多- 如何检索Ember.js模型的所有属性(How to retrieve all properties of an Ember.js model)
- maven中snapshot快照库和release发布库的区别和作用
- arraylist中的搜索元素(Search element in arraylist)
- 从mysli_fetch_array中获取选定的值并输出(Get selected value from mysli_fetch_array and output)
- Windows Phone上的可用共享扩展(Available Share Extensions on Windows Phone)
- 如何在命令提示符下将日期设置为文件名(How to set file name as date in command prompt)
- 如何在Laravel 5.2中使用paginate与关系?(How to use paginate with relationships in Laravel 5.2?)
- 从iframe访问父页面的id元素(accessing id element of parent page from iframe)
- linux的常用命令干什么用的
- Feign Client + Eureka POST请求正文(Feign Client + Eureka POST request body)
- 怎么删除禁用RHEL/CentOS 7上不需要的服务
- 为什么Gradle运行测试两次?(Why does Gradle run tests twice?)
- 由于有四个新控制器,Auth刀片是否有任何变化?(Are there any changes in Auth blades due to four new controllers?)
- 如何交换返回集中的行?(How to swap rows in a return set?)
- 在android中的活动之间切换?(Switching between activities in android?)
- Perforce:如何从Depot到Workspace丢失文件?(Perforce: how to get missing file from Depot to Workspace?)
- Webform页面避免运行服务器(Webform page avoiding runat server)
- 在ios 7中的UITableView部分周围绘制边界线(draw borderline around UITableView section in ios 7)
- 内存布局破解(memory layout hack)
- 使用Boost.Spirit Qi和Lex时的空白队长(Whitespace skipper when using Boost.Spirit Qi and Lex)
- 我们可以有一个调度程序,你可以异步添加东西,但会同步按顺序执行吗?(Can we have a dispatcher that you can add things todo asynchronously but will be executed in that order synchronously?)
- “FROM a,b”和“FROM a FULL OUTER JOIN b”之间有什么区别?(What is the difference between “FROM a, b” and “FROM a FULL OUTER JOIN b”?)
- Java中的不可变类(Immutable class in Java)
- bat批处理文件结果导出到txt
- WordPress发布查询(WordPress post query)
- 如何在关系数据库中存储与IPv6兼容的地址(How to store IPv6-compatible address in a relational database)
- 是否可以检查对象值的条件并返回密钥?(Is it possible to check the condition of a value of an object and JUST return the key?)
- 德州新起点计算机培训学校主要课程有什么?
- GEP分段错误LLVM C ++ API(GEP segmentation fault LLVM C++ API)
- “latin1_german1_ci”整理来自哪里?(Where is “latin1_german1_ci” collation coming from?)