Razor C# - Get data from database

萝らか妹 提交于 2019-12-03 17:10:22
Brandon Miller

Found the answer at Working With Data : ASP.net

@foreach(var row in db.Query("SELECT * FROM shows")) {
  <em>@row.title</em> - Cast: @row.cast
}

You are going around this the wrong way! The MVC pattern has its strengths in separating the data-logic from the actual content and right now you are retrieving new data in the View (which doesn't leave much control to the Controller)!

Have a look at http://www.asp.net/mvc for some great tutorials on how to use ASP.Net MVC and how to structure your classes and files (it will answer both your questions). It might seem a bit overdone if you used PHP and wrote every query directly in the same page before, but trust me, the code you create will be cleaner and a lot easier to maintain with ASP.Net MVC.

With reference to your side-question, I would say that using the Razor framework you cannot have a code-behind file. It mixes server-side code and client-side code together. If you want to have a code-behind file then you could use Asp.net Web Pages without the razor view engine on Visual Studio.

For the side question i believe the @function{"insert code inside this block"} will allow you to create your custom functions inside your page. Hope this helps anyone else who may have similar question and end up here. Believe it or not there are some great documentation on how to get started @ http://www.asp.net/get-started. Just go through the tutorials.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!