archive-gr.com » GR » D » DOTNETZONE.GR

Total: 987

Choose link from "Titles, links and description words view":

Or switch to "Titles and links view".
  • Dot Net Rules
    Javascript jquery LINQ LINQ to Entities LINQ to objects Linq to Sql LocalDB Localisation logging login controls master page master detail membership Mobile Views Model First MS Office objectdatasource performance POCOS Polymorphism routing scaffolding ScriptManager SEO session state silverlight Sql Server Sql Server 2005 SQL Server 2008 sql server 2012 sqlcommand stored procedures Tracing transaction log transactions UpdatePanel validation VB 2005 VB 2008 VB 9 0 ViewState Visual Basic 10 0 Visual Studio Visual Studio 2005 Visual Studio 2008 Visual Studio 2010 Visual Studio 2012 VS 2010 VS 2012 WCF web server controls web service web config window forms windows forms WPF XAML XHTML XML XML Literals Πλοήγηση Αρχική σελίδα Εκδηλώσεις Ιστολόγια Συζητήσεις Εκθέσεις Φωτογραφιών Αρχειοθήκες Ιστορικό Δημοσιεύσεων Ιούνιος 2015 4 Μάρτιος 2015 2 Ιούνιος 2014 1 Δεκέμβριος 2013 2 Νοέμβριος 2013 14 Οκτώβριος 2013 2 Σεπτέμβριος 2013 5 Ιούλιος 2013 1 Ιανουάριος 2013 8 Δεκέμβριος 2012 11 Νοέμβριος 2012 3 Οκτώβριος 2012 12 Σεπτέμβριος 2012 16 Αύγουστος 2012 6 Ιούλιος 2012 13 Ιούνιος 2012 3 Μάιος 2012 4 Απρίλιος 2012 15 Μάρτιος 2012 8 Ιανουάριος 2012 3 Δεκέμβριος 2011 7 Νοέμβριος 2011 13 Οκτώβριος 2011 6 Σεπτέμβριος 2011 8 Αύγουστος 2011 6 Ιούλιος 2011 2 Μάιος 2011 1 Μάρτιος 2011 9 Φεβρουάριος 2011 15 Ιανουάριος 2011 14 Δεκέμβριος 2010 9 Νοέμβριος 2010 2 Οκτώβριος 2010 11 Σεπτέμβριος 2010 1 Αύγουστος 2010 3 Απρίλιος 2010 2 Μάρτιος 2010 5 Ιανουάριος 2010 1 Οκτώβριος 2009 2 Σεπτέμβριος 2009 4 Ιούνιος 2009 5 Απρίλιος 2009 1 Μάρτιος 2009 3 Φεβρουάριος 2009 2 Δεκέμβριος 2008 3 Νοέμβριος 2008 10 Σεπτέμβριος 2008 1 Αύγουστος 2008 1 Ιούλιος 2008 9 Ιούνιος 2008 10 Παρουσίαση με Ετικέτες Όλες οι Ετικέτε ASPNETDB MDF RSS asp net ASP NET 4 0 C forms authentication login controls membership role providers Visual Studio 2005 Visual Studio 2008 Visual Studio 2010

    Original URL path: http://www.dotnetzone.gr/cs/blogs/dotnetrules/archive/tags/ASPNETDB.MDF/default.aspx (2015-07-15)
    Open archived version from archive


  • Dot Net Rules
    controls web service web config window forms windows forms WPF XAML XHTML XML XML Literals Πλοήγηση Αρχική σελίδα Εκδηλώσεις Ιστολόγια Συζητήσεις Εκθέσεις Φωτογραφιών Αρχειοθήκες Ιστορικό Δημοσιεύσεων Ιούνιος 2015 4 Μάρτιος 2015 2 Ιούνιος 2014 1 Δεκέμβριος 2013 2 Νοέμβριος 2013 14 Οκτώβριος 2013 2 Σεπτέμβριος 2013 5 Ιούλιος 2013 1 Ιανουάριος 2013 8 Δεκέμβριος 2012 11 Νοέμβριος 2012 3 Οκτώβριος 2012 12 Σεπτέμβριος 2012 16 Αύγουστος 2012 6 Ιούλιος 2012 13 Ιούνιος 2012 3 Μάιος 2012 4 Απρίλιος 2012 15 Μάρτιος 2012 8 Ιανουάριος 2012 3 Δεκέμβριος 2011 7 Νοέμβριος 2011 13 Οκτώβριος 2011 6 Σεπτέμβριος 2011 8 Αύγουστος 2011 6 Ιούλιος 2011 2 Μάιος 2011 1 Μάρτιος 2011 9 Φεβρουάριος 2011 15 Ιανουάριος 2011 14 Δεκέμβριος 2010 9 Νοέμβριος 2010 2 Οκτώβριος 2010 11 Σεπτέμβριος 2010 1 Αύγουστος 2010 3 Απρίλιος 2010 2 Μάρτιος 2010 5 Ιανουάριος 2010 1 Οκτώβριος 2009 2 Σεπτέμβριος 2009 4 Ιούνιος 2009 5 Απρίλιος 2009 1 Μάρτιος 2009 3 Φεβρουάριος 2009 2 Δεκέμβριος 2008 3 Νοέμβριος 2008 10 Σεπτέμβριος 2008 1 Αύγουστος 2008 1 Ιούλιος 2008 9 Ιούνιος 2008 10 Παρουσίαση με Ετικέτες Όλες οι Ετικέτε ASPxGridView RSS ArrayList asp net client side DevExpress master detail XpoDataSource How to bind a XPO Data Source to an ASPxGridView control I have been involved with an ASP Net project recently and I have implemented it using the awesome DevExpress ASP Net controls In this post I will show you how to bind an XPODataSource control to an ASPxGridView control If you want to implement this read more Posted Πέμπτη 12 Απριλίου 2012 9 18 μμ από nikolaosk 0 σχόλια Δημοσίευση στην κατηγορία asp net DevExpress ASPxGridView XpoDataSource Adding Client Side events to DevExpress ASP Net controls I have been involved in a ASP Net project recently and I have implemented it using the awesome DevExpress ASP

    Original URL path: http://www.dotnetzone.gr/cs/blogs/dotnetrules/archive/tags/ASPxGridView/default.aspx (2015-07-15)
    Open archived version from archive

  • Dot Net Rules
    Studio Visual Studio 2005 Visual Studio 2008 Visual Studio 2010 Visual Studio 2012 VS 2010 VS 2012 WCF wcf data service web server controls web service web config window forms windows forms WPF XAML XHTML XML XML Literals Πλοήγηση Αρχική σελίδα Εκδηλώσεις Ιστολόγια Συζητήσεις Εκθέσεις Φωτογραφιών Αρχειοθήκες Ιστορικό Δημοσιεύσεων Ιούνιος 2015 4 Μάρτιος 2015 2 Ιούνιος 2014 1 Δεκέμβριος 2013 2 Νοέμβριος 2013 14 Οκτώβριος 2013 2 Σεπτέμβριος 2013 5 Ιούλιος 2013 1 Ιανουάριος 2013 8 Δεκέμβριος 2012 11 Νοέμβριος 2012 3 Οκτώβριος 2012 12 Σεπτέμβριος 2012 16 Αύγουστος 2012 6 Ιούλιος 2012 13 Ιούνιος 2012 3 Μάιος 2012 4 Απρίλιος 2012 15 Μάρτιος 2012 8 Ιανουάριος 2012 3 Δεκέμβριος 2011 7 Νοέμβριος 2011 13 Οκτώβριος 2011 6 Σεπτέμβριος 2011 8 Αύγουστος 2011 6 Ιούλιος 2011 2 Μάιος 2011 1 Μάρτιος 2011 9 Φεβρουάριος 2011 15 Ιανουάριος 2011 14 Δεκέμβριος 2010 9 Νοέμβριος 2010 2 Οκτώβριος 2010 11 Σεπτέμβριος 2010 1 Αύγουστος 2010 3 Απρίλιος 2010 2 Μάρτιος 2010 5 Ιανουάριος 2010 1 Οκτώβριος 2009 2 Σεπτέμβριος 2009 4 Ιούνιος 2009 5 Απρίλιος 2009 1 Μάρτιος 2009 3 Φεβρουάριος 2009 2 Δεκέμβριος 2008 3 Νοέμβριος 2008 10 Σεπτέμβριος 2008 1 Αύγουστος 2008 1 Ιούλιος 2008 9 Ιούνιος 2008 10 Παρουσίαση με Ετικέτες Όλες οι Ετικέτε browser RSS asp net asp net 2 0 caching master page VB 2005 VB 2008 Visual Studio 2005 Visual Studio 2008 Visual Studio 2010 How to change the master page depending on the client s browser In one of my seminars on ASP Net I was asked a question from a student regarding master pages and the various browser appications He basically asked if it was possible to have a different master page depending on the client s browser My initial answer was to find the browser s version programmatically and according to this Διαβάστε

    Original URL path: http://www.dotnetzone.gr/cs/blogs/dotnetrules/archive/tags/browser/default.aspx (2015-07-15)
    Open archived version from archive

  • Dot Net Rules
    FirstName data LastName footballer text str fail function jqXHR textStatus err footballer text Error err script head body id body div class main div h1 All Footballers h1 ul id footballers div div label for FootballerId ID label input type text id FootballerId size 5 input type button value Search onclick find p id footballer div div body html In the second part next blog post I will be extending the application to insert update and delete information Hope it helps Posted Πέμπτη 10 Ιανουαρίου 2013 12 40 πμ από nikolaosk 0 σχόλια Δημοσίευση στην κατηγορία asp net C Entity framework jquery Code First Visual Studio 2012 ASP NET MVC 4 0 ASP NET Web Api Creating a complete ASP Net MVC 4 0 application with Visual Studio 2012 C EF 5 0 Code First part 6 I have decided to write a series of posts on how to write a small ASP Net MVC 4 0 application I will develop this application step by step and I will explain everything that you need to know in order to develop ASP Net MVC 4 0 applications This is the sixth post in this series You can find the first one here the second one here third one here the fourth one here and the fifth one here Make sure you read and understand those posts In this post I will add some validations to our application through Code First Data Annotations and migrate those changes to our database through EF Code First Migrations Right now there is no validation for the fields Rating and Comment of the MovieReview entity I want to have a validation rule applied to the Rating field that will accept only values 1 to 10 and it will be mandatory The Comment field will only be 100 characters long and also mandatory We also want to have the characters for the Name property of the Movie entity restricted to 70 and the name of the Director restricted to 50 characters Both of these properties should be mandatory I am going briefly to introduce Data Annotations and how to use them in our Code First Entity framework applications I have developed my data access layer with EF We have 3 development paradigms in EF We have Database First Model First and Code First The last one Code First is the one I have used for this application and frankly speaking it is gaining in popularity amongst developers I will use another post also found in this blog to demonstrate Data Annotations In order to fully understand what I am talking about you need to read this post titled Using the Code First approach when building ASP Net applications with Entity Framework It will take some time to create this application but it is necessary in order to follow along With Data Annotations we can configure our domain entity classes so that they can take best advantage of the EF We can decorate our entity classes with declarative attributes Let me give you an insight on how EF Code First works EF Code First at run time looks at the entity domain classes and infers from them the in memory data that it needs to interpret the queries and interact with the database For example it assumes that any property named ID represents the key property of the class Data Annotations live inside the System ComponentModel DataAnnotations We do add Data Annotations to our domain classes declaratively using attributes You can also do that imperatively using the Fluent API 1 Launch Visual Studio and launch the ASP Net MVC 4 0 application we have been developing so far 2 Have a look at my entity class Movie public class Μovie public int Id get set public string Name get set public string Director get set public DateTime YearReleased get set public virtual ICollection MovieReview Reviews get set If I go to the entity above and make one little change renaming the public string Name get set to Required MaxLength 70 public string Name get set my application will get an error if compile and view it in a browser It will compile but when I click on the Movie link on the menu I get the results shown in the following picture 3 Make sure you add a reference to the System ComponentModel DataAnnotations namespace in the class file using System ComponentModel DataAnnotations Both entities after applying the data annotation attributes follow public class Μovie public int Id get set Required MaxLength 70 public string Name get set Required MaxLength 50 public string Director get set public DateTime YearReleased get set public virtual ICollection MovieReview Reviews get set public class MovieReview public int Id get set Required Range 1 10 public int Rating get set Required MaxLength 50 public string Comment get set public int MovieId get set There are other data annotation attributes like Key ConcurrencyCheck Table MyTable Schema guest but it is impossible to cover everything in this post Now I am compiling again my application and then when I click on the Movie link on the menu I get the results shown in the following picture So we receive an error Well there is an explanation for that behavior The Entity Framework always checks the model that is in effect that we just configured against the model it used originally to create the database 4 The Entity Framework detected that there is something different in this model When we apply the Required attribute to an property in the entity then this field in the database should be not null In order to solve that problem we must apply Code First Migrations Code First Migrations is an Entity Framework feature introduced in version 4 3 back in February of 2012 Before the addition of Code First Migrations 4 1 4 2 versions Code First database initialisation meant that Code First would create the database if it does not exist the default behaviour CreateDatabaseIfNotExists The other pattern we could use is DropCreateDatabaseIfModelChanges which means that Entity Framework will drop the database if it realises that model has changes since the last time it created the database The final pattern is DropCreateDatabaseAlways which means that Code First will recreate the database every time one runs the application That is of course fine for the development database but totally unacceptable and catastrophic when you have a production database We cannot lose our data because of the way that Code First works Migrations solve this problem With migrations we can modify the database without completely dropping it We can modify the database schema to reflect the changes to the model without losing data 5 EF Code First Migrations is not activated by default We have to activate them manually and configure them according to our needs We will open the Package Manager Console from the Tools menu within Visual Studio Then we will activate the EF Code First Migration Features by writing the command Enable Migrations Have a look at the picture below This adds a new folder Migrations in our project A new auto generated class Configuration cs is created Another class is also created CURRENTDATE InitialCreate cs and added to our project The Configuration cs is shown in the picture below 6 Now we need to update the database In the Configurations cs I change the AutomaticMigrationsEnabled value to true public Configuration AutomaticMigrationsEnabled true Then in the Package Manager Console we write the following Update Database Verbose This will fail because EF understands that we take a column that was nvarchar max and make it nvarchar 50 e t c Then in the Package Manager Console we write the following Update Database Verbose Force This statement will succeed That will force the changes to the database Have a look at the picture below As you can see all the changes were made to the database Now if we run again our application and test it try to violate the 1 10 values allowed in the rating field I will get the error shown in the picture below I know we covered a lot in this post but you must understand it if you want to master EF Code First Hope it helps Posted Τετάρτη 2 Ιανουαρίου 2013 1 17 πμ από nikolaosk 0 σχόλια Δημοσίευση στην κατηγορία asp net C CSS Entity framework jquery HTML Code First Visual Studio 2012 ASP NET MVC 4 0 Creating a complete ASP Net MVC 4 0 application with Visual Studio 2012 C EF 5 0 Code First part 5 I have decided to write a series of posts on how to write a small ASP Net MVC 4 0 application I will develop this application step by step and I will explain everything that you need to know in order to develop ASP Net MVC 4 0 applications This is the fourth post in this series You can find the first one here the second one here the third one here and the fourth one here Make sure you read and understand those posts In this post I will add Search functionality to my application and 1 Launch Visual Studio and open the application 2 We must add code to the Layout cshtml view in the Shared folder and more specifically to the menu We must add an entry so someone can navigate to the Movie View The nav section becomes like this nav ul id menu li Html ActionLink Home Index Home li li Html ActionLink About About Home li li Html ActionLink Contact Contact Home li li Html ActionLink Movie Index Movie li ul nav 3 Now we must delete the Authorize Action Filter attribute from the public ActionResult Index that we previously entered Now if we run the application we will see a new link in the menu Movie Have a look at the picture below 5 Now we will add search functionality in our application We need to add some code to the MovieController cs file We will add another public method Search that gets an input parameter looks for name of the movie The code is very easy to follow I just use standard LINQ syntax public ActionResult Search string searchString var movies from movie in db Movies select movie if String IsNullOrEmpty searchString movies movies Where m m Name Contains searchString return View movies Now we need to implement the corresponding view Right click on the public ActionResult Search string searchString and select Add View Have a look at the picture below to see the settings you must add in the popup window Add View Click Add Have a look at the Search cshtml file that was created inside the Views Movie folder Have a look at the generated code You will see HTML helper objects and methods Run your application and navigate to Movie Search Append a query string such as searchString godfather to the URL The filtered entries y are displayed Have a look at the picture below 6 Now we need some sort of user interface so that the user can enter the search string I am going to make some changes to the Views Movie Search cshtml view Open the file and under the p Html ActionLink Create New Create add the following lines using Html BeginForm p Title Html TextBox SearchString br input type submit value Search p p We have the Html BeginForm helper method that creates an opening form tag The helper method causes the form to post to itself when the user submits the form by clicking the Search button Have a look at the picture below Have a look at the picture below Up to this point we have an application that one can add movies name director and the year released edit and delete movies search for movies by entering the name of the movie Well me must move on and build the Reviews functionality the ability for someone to write and edit a review for a particular movie 7 When I click on the Details link on the http localhost 59871 Movie I get the following screen This is not very useful so I will delete the Details View in the Movie folder I will also comment out the Details method in the MovieController cs GET Movie Details 5 public ActionResult Details int id 0 Μovie movie db Movies Find id if movie null return HttpNotFound return View movie I will also change the Index cshtml in the Movie folder I will replace the Details with the Reviews in the HTML ActionLink td Html ActionLink Edit Edit new id item Id Html ActionLink Reviews Index Reviews new id item Id null Html ActionLink Delete Delete new id item Id td 8 Now we need to implement the ReviewsController controller so we can begin creating editing deleting reviews and associating them with movies Right click the Controllers folder and create a new ReviewsController controller Have a look at the picture below to set the appropriate settings Click Add Visual Studio will create the following A ReviewsController cs file in the project s Controllers folder A Reviews folder in the project s Views folder Create cshtml Delete cshtml Details cshtml Edit cshtml and Index cshtml in the new Views Reviews folder 9 Now we must change some of the code that was generated by the scaffolding I will open the ReviewsController cs and change the Index method public class ReviewsController Controller private MovieDBContext db new MovieDBContext GET Reviews public ActionResult Index Bind Prefix id int movieid var movie db Movies Find movieid if movie null return View movie return HttpNotFound As you can see this Action method must receive a parameter This parameter represents a movie so we pass a movieid to this method By using this Bind Prefix id I just alias id with movieid 10 Now we need to make some changes in the Index cshtml in the Views Reviews folder since we pass a movieid and not a reviewid We must delete most of the code The contents of the Index cshtml should be model MovieReviews Models Μovie ViewBag Title Index h2 Reviews for Model Name h2 p Html ActionLink Create New Create p That should be all Now I am going to create a partial view Reviews Have a look at the picture below Click Add 11 Now we must change the code in the partial view Reviews Have a look at the picture below to see the complete implementation The code inside the table table tags is the code I commented out in the Index cshtml in the Reviews folder Now I must call this partial view from the Index cshtml in the Reviews folder I add the code in bold h2 Reviews for Model Name h2 Html Partial Reviews Model Reviews 12 Now we need a way to associate the review to a movie when clicking on the link Create New for the review p Html ActionLink Create New Create new movieid Model Id p Now we build and run the application again When I click on the Movie link http localhost 59871 Movie in the menu I see the Reviews link for both my movies I choose to click on the Reviews link for the first movie The GodFather and when I click on it I see the following screen where I can enter my review after hitting Create New Finally I hit the Create button and my review for that movie has been entered Have a look at the picture below This is the code for the Create HTTPGet action inside the ReviewsController cs GET Reviews Create HttpGet public ActionResult Create int movieid return View We pass it the movieid for that movie as a parameter This is the code for the Create HttpPost action inside the ReviewsController cs POST Reviews Create HttpPost public ActionResult Create MovieReview moviereview if ModelState IsValid db Reviews Add moviereview db SaveChanges return RedirectToAction Index new id moviereview MovieId return View moviereview This method will post back the form to the same url it came from 13 Now let s see how to change slightly the code generated for the Edit method in the ReviewsController cs public ActionResult Edit int id MovieReview moviereview db Reviews Find id if moviereview null return HttpNotFound return View moviereview POST Reviews Edit 5 HttpPost public ActionResult Edit MovieReview moviereview if ModelState IsValid db Entry moviereview State EntityState Modified db SaveChanges return RedirectToAction Index new id moviereview MovieId return View moviereview This method will post back the form to the same url it came from Now we build and run the application again When I click on the Movie link http localhost 59871 Movie in the menu I see the Reviews link for both my movies I choose to click on the Reviews link for the first movie The GodFather and when I click on it I can see the reviews and hit the Edit link Have a look at the picture below Finally I hit the Save button and my review for that movie has been edited 14 Now let s see how to change slightly the code generated for the Delete method in the ReviewsController cs GET Reviews Delete 5 public ActionResult Delete int id MovieReview moviereview db Reviews Find id if moviereview null return HttpNotFound return View moviereview POST Reviews Delete 5 HttpPost ActionName Delete public ActionResult DeleteConfirmed int id MovieReview moviereview db Reviews Find id db Reviews Remove moviereview db SaveChanges return RedirectToAction Index new id moviereview MovieId This method will post back the form to the same url it came from Now we build and run the application again When I click on the Movie link http localhost 59871 Movie in the menu I see the Reviews link for both my movies I choose to click on the Reviews link for the first movie The GodFather and when I click on it I can see the reviews and hit the Delete link Have a look at the picture below Finally I hit the Delete button and my review for that movie has been deleted Do not underestimate what we have accomplished so far We have managed to develop an ASP Net MVC 4 0 application where one can create edit delete view and search for movies He can also create edit delete view reviews for a particular movie In the next post I will talk about validation through data annotations and database migrations within the context of the Entity Framework Code First Paradigm Hope it helps Posted Τρίτη 1 Ιανουαρίου 2013 5 19 μμ από nikolaosk 0 σχόλια Δημοσίευση στην κατηγορία asp net C CSS Entity framework jquery HTML Code First Visual Studio 2012 ASP NET MVC 4 0 Creating a complete ASP Net MVC 4 0 application with Visual Studio 2012 C EF 5 0 Code First part 4 I have decided to write a series of posts on how to write a small ASP Net MVC 4 0 application I will develop this application step by step and I will explain everything that you need to know in order to develop ASP Net MVC 4 0 applications This is the fourth post in this series You can find the first one here the second one here and the third one here Make sure you read and understand those posts I am building an ASP Net MVC application where users can enter movies and rate them As we develop our application I will change the requirements and add more features My goal is to have a working application and at the same time show you the building blocks of ASP Net MVC In the last post we have created the entities needed in order to store and retrieve data models views and the database Now I would like to talk about Code Blocks and Code Expressions in Razor Views 1 Launch Visual Studio and open the ASP Net MVC application 2 Open the Index cshtml from the Views Movie folder These are the contents of the view model IEnumerable MovieReviews Models Μovie ViewBag Title Index h2 Index h2 p Html ActionLink Create New Create p table tr th Html DisplayNameFor model model Name th th Html DisplayNameFor model model Director th th Html DisplayNameFor model model YearReleased th th th tr foreach var item in Model tr td Html DisplayFor modelItem item Name td td Html DisplayFor modelItem item Director td td Html DisplayFor modelItem item YearReleased td td Html ActionLink Edit Edit new id item Id Html ActionLink Details Details new id item Id Html ActionLink Delete Delete new id item Id td tr table The responsibility of this view is to take the model passed by the controller and present the data to the users through the template The View is nothing more than a template where the data passed from the model is presented We combine literal text e g h2 h2 with pieces of data from the model through C code Code Expressions An example of code expressions from the view above td Html DisplayFor modelItem item Name td The code in bold is a code expression The code expression will be evaluated by the Razor engine and the output which will be displayed in a column of an html table will be presented to the client Code blocks An example of code block from the view above ViewBag Title Index Between the curly braces we can add as much C code as we want Let me modify a little bit the View above ViewBag Title Index var reviewscount Model Count h2 Index h2 h3 the number of the reviews so far is reviewscount h3 The code I changed is in bold Run your application again http localhost 59871 movie http localhost yourport movie in your case I added a new variable inside the code block and then used the variable reviewscount in a place I wanted inside the view as a code expression Hope all makes sense so far 3 Now I would like to point out at this point are HTML Helpers that are part of any View in the ASP Net MVC applications Their purpose is to create small blocks of HTML There are helpers like to create links inputs labels forms validation messages and much more Think of them like traditional ASP NET Web Form controls We use HTML helpers are used to modify HTML HTML helpers do not have an event model and a view state In most cases an HTML helper is just a method that returns a string Let s have a look at the Html ActionLink Html ActionLink Edit Edit new id item Id This is the easiest way to render an HTML link The Html ActionLink does not link to a view It creates a link to a controller action You also can see more HTML Helpers in the View Html DisplayNameFor which Gets the display name for the model Html DisplayFor will render the DisplayTemplate that matches the property s type There are more HTML helpers can be used to render modify and output HTML form elements BeginForm EndForm TextArea TextBox ValidationMessageFor We will see more of those as we move on 3 When you run your application and navigate to the http localhost 59871 movie you see HTML elements that are not in the current view Index view I mean the navigation system menu the header logo and the footer Where does this markup come from It comes form the Layout View Υοu can think of this as the master pages in web forms applications Have a look at the picture below to see what I mean Have a look in the Shared folder in the Solution Explorer You will see the Layout cshtml DOCTYPE html html lang en head meta charset utf 8 title ViewBag Title My ASP NET MVC Application title link href favicon ico rel shortcut icon type image x icon meta name viewport content width device width Styles Render Content css Scripts Render bundles modernizr head body header div class content wrapper div class float left p class site title Html ActionLink your logo here Index Home p div div class float right section id login Html Partial LoginPartial section nav ul id menu li Html ActionLink Home Index Home li li Html ActionLink About About Home li li Html ActionLink Contact Contact Home li ul nav div div header div id body RenderSection featured required false section class content wrapper main content clear fix RenderBody section div footer div class content wrapper div class float left p copy DateTime Now Year My ASP NET MVC Application p div div footer Scripts Render bundles jquery RenderSection scripts required false body html We have the head section that will be present in all pages head meta charset utf 8 title ViewBag Title My ASP NET MVC Application title link href favicon ico rel shortcut icon type image x icon meta name viewport content width device width Styles Render Content css Scripts Render bundles modernizr head Then you have the header section that will appear in all pages header div class content wrapper div class float left p class site title Html ActionLink your logo here Index Home p div div class float right section id login Html Partial LoginPartial section nav ul id menu li Html ActionLink Home Index Home li li Html ActionLink About About Home li li Html ActionLink Contact Contact Home li ul nav div div header Then you have the header section that will appear in all pages footer div class content wrapper div class float left p copy DateTime Now Year My ASP NET MVC Application p div div footer At one point we see a call to the RenderBody div id body RenderSection featured required false section class content wrapper main content clear fix RenderBody section When the Layout View calls RenderBody that is when the content View index cshtml will be inserted and rendered at this exact point in the HTML markup How does ASP Net MVC to call the Layout View before our content View It is because of this file ViewStart cshtml The contents of the ViewStart cshtml follow Layout Views Shared Layout cshtml ASP Net MVC runtime knows to render first the Layout View because the ViewStart cshtml runs first before any other view and then the Content View 4 Another important topic in ASP Net MVC applications are Partial Views At some point in the Layout cshtml view there is a call to the LoginPartial partial view section id login Html Partial LoginPartial section A partial view is a view that we place code HTML C that we will often reuse in other views Have a look at the contents of the LoginPartial cshtml if Request IsAuthenticated text Hello Html ActionLink User Identity Name Manage Account routeValues null htmlAttributes new class username title Manage using Html BeginForm LogOff Account FormMethod Post new id logoutForm Html AntiForgeryToken a href BLOCKED SCRIPTdocument getElementById logoutForm submit Log off a text else ul li Html ActionLink Register Register Account routeValues null htmlAttributes new id registerLink li li Html ActionLink Log in Login Account routeValues null htmlAttributes new id loginLink li ul So this is a type of special View that we must know and use so we make our application easier to maintain 5 Now it is time to have a look at some important topics that are related with Controllers Open the MovieController cs file I would like to talk a bit about ActionResults and what thet are The ActionResult is an abstract base class for other types of actions Other classes inheriting from the ActionResult ContentResult EmptyResult FileResult HttpStatusCodeResult JavaScriptResult RedirectResult RedirectToRouteResult ViewResult The ViewResult renders a specifed view to the client The RedirectResult performs an HTTP redirection to a specified URL The ContentResult writes content to the client without requiring a view In this method Index the result of this method is ActionResult which means we will call the Index cshtml passing the model to it in the Views Movie folder So the results of this view will be displayed to the client private MovieDBContext db new MovieDBContext GET Movie public ActionResult Index return View db Movies ToList Another very important topic is Action Selectors Some of them are ActionName AcceptVerbs An ActionSelector dictates which action method is triggered They come with the form of attributes Have a look at the MovieController cs file and the Delete method HttpPost ActionName Delete public ActionResult DeleteConfirmed int id Μovie μovie db Movies Find id db Movies Remove μovie db SaveChanges return RedirectToAction Index ActionName will specify the name of this method It can be reached by Delete and not DeleteConfirmed AcceptVerbs HttpPost HttpGet represent attributes that specify which HTTP verbs Get Post an action method will respond to In the example above we have an HTTP Post HttpPost ActionName Delete The final topic I would like to talk about is Action Filters They are applied to methods and classes in the form of attributes An ActionFilter provides some methods that are run before and after request and response processing Some of them are OutputCache which caches the output of the controller Authorize that restricts an action to authorized users or roles If I open the MovieController cs file and then apply the Authorize see below to the Index method private MovieDBContext db new MovieDBContext GET Movie Authorize public ActionResult Index return View db Movies ToList and then navigate to the http localhost 59871 movie this is what I will get the following as a result redirect to the login page The ASP Net MVC runtime will pick up the Authorize attribute and redirect me to the Login page Hope you have followed along and mastered the topics presented here as they are absolutely necessary for building ASP Net MVC applications In the next post we will continue building our application Hope it helps Posted Κυριακή 30 Δεκεμβρίου 2012 11 37 μμ από nikolaosk 0 σχόλια Δημοσίευση στην κατηγορία asp net C CSS Entity framework jquery HTML Code First Visual Studio 2012 ASP NET MVC 4 0 Creating a complete ASP Net MVC 4 0 application with Visual Studio 2012 C EF 5 0 Code First part 3 I have decided to write a series of posts on how to write a small ASP Net MVC 4 0 application I will develop this application step by step and I will explain everything that you need to know in order to develop ASP Net MVC 4 0 applications This is the third post in this series You can find the first one here and the second one here Make sure you read and understand the first post and the second post As I design and develop the application I will explain some of the most common building blocks of ASP Net MVC like Code blocks Code expressions Action Results Action Selectors Action Filters Layout Views and Partial Views Now we have to think about the data access technology that we will use in our sample application I am going to build an ASP Net MVC application where users can search through a collection of movies and rate them I will not use traditional ADO Net data access techniques I will use Entity Framework EF which is part of the Net framework Obviously I cannot go into much detail on what EF is and what it does I will give again a short introduction The Net framework provides support for Object Relational Mapping through EF So EF is a an ORM tool and it is now the main data access technology that microsoft works on I use it quite extensively in my projects Through EF we have many things out of the box provided for us We have the automatic generation of SQL code It maps relational data to strongly types objects All the changes made to the objects in the memory are persisted in a transactional way back to the data store You can search in my blog because I have posted many posts regarding ASP Net and EF There are different approaches paradigms available using the Entity Framework namely Database First Code First Model First You can find in this post an example on how to use the Entity Framework to retrieve data from an SQL Server Database using the Database Schema First approach In this approach we make all the changes at the database level and then we update the model with those changes In this post you can see an example on how to use the Model First approach when working with ASP Net and the Entity Framework This model was firstly introduced in EF version 4 0 and we could start with a blank model and then create a database from that model When we made changes to the model we could recreate the database from the new model The Code First approach is the more code centric than the other two Basically we write POCO classes and then we persist to a database using something called DBContext In this application we will us the Code First approach when building our data centric application with EF Code First relies on DbContext We create 2 3 classes e g Movie Review with properties and then these classes interact with the DbContext class Then we can create a new database based upon our POCOS classes and have tables generated from those classes We do not have an edmx file in this approach By using this approach we can write much easier unit tests DbContext is a new context class and is smaller lightweight wrapper for the main context class which is ObjectContext Schema First and Model First When building an application the most important thing is to understand the domain and the domain business objects These should be the objects that should drive the application and not e g the database schema So I like to design my entities very carefully and 1 Launch Visual Studio and open your application 2 We will add the 2 entities I am going to show you below in the Models folder Add a new class file named Movie cs inside the Models folder The Movie cs entity is as follows public class Μovie public int Id get set public string Name get set public string Director get set public DateTime YearReleased get set public ICollection MovieReview Reviews get set We have added some properties in this entity Now we need to add the MovieReview entity Add a new class file named MovieReview cs inside the Models folder The MovieReview cs entity is as follows public class MovieReview public int Id get set public int Rating get set public string Comment get set public int MovieId get set So we have our entities ready We have one movie and many reviews That should be clear by now I will instantiate these objects store them and retrieve them in my database My database will be an SQL Server database where I will create from the entities Then we need to create a context class that inherits from DbContext Add a new class to the Models folder Name it MovieDBContext cs Now that we have the entity classes created we must let the model know I will have to use the DbSet T property The code for this class follows public class MovieDBContext DbContext public DbSet Μovie Movies get set public DbSet MovieReview Reviews get set The MovieDBContext is a database context class This class is responsible for talking to the underlying database storing and updating the data to the database We need to add this reference to the file using System Data Entity Now we need to create the connection string The only place we can do that is by opening the web config file and adding the following lines of code inside the connectionStrings section connectionStrings add name FootballerDBContext connectionString

    Original URL path: http://www.dotnetzone.gr/cs/blogs/dotnetrules/archive/tags/C_2300_/default.aspx (2015-07-15)
    Open archived version from archive

  • Dot Net Rules
    Office objectdatasource performance POCOS Polymorphism routing scaffolding ScriptManager SEO session state silverlight Sql Server Sql Server 2005 SQL Server 2008 sql server 2012 sqlcommand stored procedures Tracing transaction log transactions UpdatePanel validation VB 2005 VB 2008 VB 9 0 ViewState Visual Basic 10 0 Visual Studio Visual Studio 2005 Visual Studio 2008 Visual Studio 2010 Visual Studio 2012 VS 2010 VS 2012 WCF web server controls web service web config window forms windows forms WPF XAML XHTML XML XML Literals Πλοήγηση Αρχική σελίδα Εκδηλώσεις Ιστολόγια Συζητήσεις Εκθέσεις Φωτογραφιών Αρχειοθήκες Ιστορικό Δημοσιεύσεων Ιούνιος 2015 4 Μάρτιος 2015 2 Ιούνιος 2014 1 Δεκέμβριος 2013 2 Νοέμβριος 2013 14 Οκτώβριος 2013 2 Σεπτέμβριος 2013 5 Ιούλιος 2013 1 Ιανουάριος 2013 8 Δεκέμβριος 2012 11 Νοέμβριος 2012 3 Οκτώβριος 2012 12 Σεπτέμβριος 2012 16 Αύγουστος 2012 6 Ιούλιος 2012 13 Ιούνιος 2012 3 Μάιος 2012 4 Απρίλιος 2012 15 Μάρτιος 2012 8 Ιανουάριος 2012 3 Δεκέμβριος 2011 7 Νοέμβριος 2011 13 Οκτώβριος 2011 6 Σεπτέμβριος 2011 8 Αύγουστος 2011 6 Ιούλιος 2011 2 Μάιος 2011 1 Μάρτιος 2011 9 Φεβρουάριος 2011 15 Ιανουάριος 2011 14 Δεκέμβριος 2010 9 Νοέμβριος 2010 2 Οκτώβριος 2010 11 Σεπτέμβριος 2010 1 Αύγουστος 2010 3 Απρίλιος 2010 2 Μάρτιος 2010 5 Ιανουάριος 2010 1 Οκτώβριος 2009 2 Σεπτέμβριος 2009 4 Ιούνιος 2009 5 Απρίλιος 2009 1 Μάρτιος 2009 3 Φεβρουάριος 2009 2 Δεκέμβριος 2008 3 Νοέμβριος 2008 10 Σεπτέμβριος 2008 1 Αύγουστος 2008 1 Ιούλιος 2008 9 Ιούνιος 2008 10 Παρουσίαση με Ετικέτες Όλες οι Ετικέτε C 3 0 RSS NET 3 5 Framework asp net C c 3 0 LINQ Linq to Sql Object oriented programming OOP SQL Server 2008 Visual Studio 2008 Object Oriented Programming Concepts with C 3 0 In this post I will try to explain Object Oriented programming model paradigm Some of the topics

    Original URL path: http://www.dotnetzone.gr/cs/blogs/dotnetrules/archive/tags/C_2300_3.0/default.aspx (2015-07-15)
    Open archived version from archive

  • Dot Net Rules
    for EF 6 1 applications Entity Framework is an object relational mapping ORM framework for the NET Framework EF addresses the problem of Object relational impedance mismatch I will not be talking about that mismatch because it is well documented in many sites on the Internet Through that framework we can program against a conceptual application model instead of programming directly against a relational schema model By doing so we can decrease the amount of code we do write to access a data storage and thus decrease maintenance time You can find many posts regarding Entity Framework in this blog 1 I will create an empty ASP Net Application Web Forms Application and I give it the name EFSecondLevelCaching I am using Visual Studio 2013 Ultimate edition 2 I add a new web forms page in the application Leave the default name Αdd a Gridview control to the web form 3 I will use the AdventureWorks2014 database You can download it here for this application and more specifically the Person Person table I have installed SQL Server 2014 Enterprise edition in my machine SQL Express edition will work fine 4 I will add a class library project to the solution namely DataAccess I will add an ADO Net Entity data model using Database First to the class library project Follow the wizzard steps create the connection string and then import into the conceptual model the Person Person table which will become an entity in the domain model In the am going to install the EntityFramework Cache from the Nuget Library in this project 5 I will write a sql statement to acquire rows from the Person Person table Connect to your SQL Server instance Type the following query SELECT FROM AdventureWorks2014 Person Person WHERE FirstName Rob This query after execution will return 5 rows 6 In the webform1 aspx cs file public partial class WebForm1 I will create a new method that returns the persons with FirstName Rob private static List Person SelectAll AdventureWorks2014Entities ctx new AdventureWorks2014Entities var query from p in ctx People where p FirstName Rob orderby p BusinessEntityID ascending select p return query ToList Ι just return all the persons with the first name Rob from the database 7 In our webform in the Page Load event we write the following code protected void Page Load object sender EventArgs e GridView1 DataSource SelectAll GridView1 DataBind Response Write hr Make sure you build your application but do not run it just yet Make sure that you copy the connection string from the App config to the Web config connectionStrings add name AdventureWorks2014Entities connectionString metadata res Model1 csdl res Model1 ssdl res Model1 msl provider System Data SqlClient provider connection string quot data source initial catalog AdventureWorks2014 integrated security True MultipleActiveResultSets True App EntityFramework quot providerName System Data EntityClient connectionStrings 8 I will use the Sql Server Profiler to see what queries are hitting my database I launch the SQL Server Profiler I connect to the SQL Server Enterprise

    Original URL path: http://www.dotnetzone.gr/cs/blogs/dotnetrules/archive/tags/caching/default.aspx (2015-07-15)
    Open archived version from archive

  • Dot Net Rules
    LINQ LINQ to Entities Linq to Sql LocalDB logging login controls master page master detail membership Mobile Views Model First MS Office objectdatasource performance Polymorphism SEO session state silverlight Sql Server Sql Server 2005 SQL Server 2008 sql server 2012 sqlcommand SQLDataReader stored procedures tables tips and tricks Tracing transaction log transactions UpdatePanel validation VB 2005 VB 2008 VB 9 0 ViewState Visual Basic 10 0 Visual Studio Visual Studio 2005 Visual Studio 2008 Visual Studio 2010 Visual Studio 2012 VS 2010 VS 2012 WCF wcf data service web server controls web service web config window forms windows forms WPF XAML XHTML XML XML Literals Πλοήγηση Αρχική σελίδα Εκδηλώσεις Ιστολόγια Συζητήσεις Εκθέσεις Φωτογραφιών Αρχειοθήκες Ιστορικό Δημοσιεύσεων Ιούνιος 2015 4 Μάρτιος 2015 2 Ιούνιος 2014 1 Δεκέμβριος 2013 2 Νοέμβριος 2013 14 Οκτώβριος 2013 2 Σεπτέμβριος 2013 5 Ιούλιος 2013 1 Ιανουάριος 2013 8 Δεκέμβριος 2012 11 Νοέμβριος 2012 3 Οκτώβριος 2012 12 Σεπτέμβριος 2012 16 Αύγουστος 2012 6 Ιούλιος 2012 13 Ιούνιος 2012 3 Μάιος 2012 4 Απρίλιος 2012 15 Μάρτιος 2012 8 Ιανουάριος 2012 3 Δεκέμβριος 2011 7 Νοέμβριος 2011 13 Οκτώβριος 2011 6 Σεπτέμβριος 2011 8 Αύγουστος 2011 6 Ιούλιος 2011 2 Μάιος 2011 1 Μάρτιος 2011 9 Φεβρουάριος 2011 15 Ιανουάριος 2011 14 Δεκέμβριος 2010 9 Νοέμβριος 2010 2 Οκτώβριος 2010 11 Σεπτέμβριος 2010 1 Αύγουστος 2010 3 Απρίλιος 2010 2 Μάρτιος 2010 5 Ιανουάριος 2010 1 Οκτώβριος 2009 2 Σεπτέμβριος 2009 4 Ιούνιος 2009 5 Απρίλιος 2009 1 Μάρτιος 2009 3 Φεβρουάριος 2009 2 Δεκέμβριος 2008 3 Νοέμβριος 2008 10 Σεπτέμβριος 2008 1 Αύγουστος 2008 1 Ιούλιος 2008 9 Ιούνιος 2008 10 Παρουσίαση με Ετικέτες Όλες οι Ετικέτε concurrency RSS asp net ASP NET 4 0 aspects of transactions blocking C c 3 0 deadlocking Entity framework lock modes locking trace flags transactions types of transactions

    Original URL path: http://www.dotnetzone.gr/cs/blogs/dotnetrules/archive/tags/concurrency/default.aspx (2015-07-15)
    Open archived version from archive

  • Dot Net Rules
    edit delete view reviews for a particular movie In the next post I will talk about validation through data annotations and database migrations within the context of the Entity Framework Code First Paradigm Hope it helps Posted Τρίτη 1 Ιανουαρίου 2013 5 19 μμ από nikolaosk 0 σχόλια Δημοσίευση στην κατηγορία asp net C CSS Entity framework jquery HTML Code First Visual Studio 2012 ASP NET MVC 4 0 Creating a complete ASP Net MVC 4 0 application with Visual Studio 2012 C EF 5 0 Code First part 4 I have decided to write a series of posts on how to write a small ASP Net MVC 4 0 application I will develop this application step by step and I will explain everything that you need to know in order to develop ASP Net MVC 4 0 applications This is the fourth post in this series You can find the first one here the second one here and the third one here Make sure you read and understand those posts I am building an ASP Net MVC application where users can enter movies and rate them As we develop our application I will change the requirements and add more features My goal is to have a working application and at the same time show you the building blocks of ASP Net MVC In the last post we have created the entities needed in order to store and retrieve data models views and the database Now I would like to talk about Code Blocks and Code Expressions in Razor Views 1 Launch Visual Studio and open the ASP Net MVC application 2 Open the Index cshtml from the Views Movie folder These are the contents of the view model IEnumerable MovieReviews Models Μovie ViewBag Title Index h2 Index h2 p Html ActionLink Create New Create p table tr th Html DisplayNameFor model model Name th th Html DisplayNameFor model model Director th th Html DisplayNameFor model model YearReleased th th th tr foreach var item in Model tr td Html DisplayFor modelItem item Name td td Html DisplayFor modelItem item Director td td Html DisplayFor modelItem item YearReleased td td Html ActionLink Edit Edit new id item Id Html ActionLink Details Details new id item Id Html ActionLink Delete Delete new id item Id td tr table The responsibility of this view is to take the model passed by the controller and present the data to the users through the template The View is nothing more than a template where the data passed from the model is presented We combine literal text e g h2 h2 with pieces of data from the model through C code Code Expressions An example of code expressions from the view above td Html DisplayFor modelItem item Name td The code in bold is a code expression The code expression will be evaluated by the Razor engine and the output which will be displayed in a column of an html table will be presented to the client Code blocks An example of code block from the view above ViewBag Title Index Between the curly braces we can add as much C code as we want Let me modify a little bit the View above ViewBag Title Index var reviewscount Model Count h2 Index h2 h3 the number of the reviews so far is reviewscount h3 The code I changed is in bold Run your application again http localhost 59871 movie http localhost yourport movie in your case I added a new variable inside the code block and then used the variable reviewscount in a place I wanted inside the view as a code expression Hope all makes sense so far 3 Now I would like to point out at this point are HTML Helpers that are part of any View in the ASP Net MVC applications Their purpose is to create small blocks of HTML There are helpers like to create links inputs labels forms validation messages and much more Think of them like traditional ASP NET Web Form controls We use HTML helpers are used to modify HTML HTML helpers do not have an event model and a view state In most cases an HTML helper is just a method that returns a string Let s have a look at the Html ActionLink Html ActionLink Edit Edit new id item Id This is the easiest way to render an HTML link The Html ActionLink does not link to a view It creates a link to a controller action You also can see more HTML Helpers in the View Html DisplayNameFor which Gets the display name for the model Html DisplayFor will render the DisplayTemplate that matches the property s type There are more HTML helpers can be used to render modify and output HTML form elements BeginForm EndForm TextArea TextBox ValidationMessageFor We will see more of those as we move on 3 When you run your application and navigate to the http localhost 59871 movie you see HTML elements that are not in the current view Index view I mean the navigation system menu the header logo and the footer Where does this markup come from It comes form the Layout View Υοu can think of this as the master pages in web forms applications Have a look at the picture below to see what I mean Have a look in the Shared folder in the Solution Explorer You will see the Layout cshtml DOCTYPE html html lang en head meta charset utf 8 title ViewBag Title My ASP NET MVC Application title link href favicon ico rel shortcut icon type image x icon meta name viewport content width device width Styles Render Content css Scripts Render bundles modernizr head body header div class content wrapper div class float left p class site title Html ActionLink your logo here Index Home p div div class float right section id login Html Partial LoginPartial section nav ul id menu li Html ActionLink Home Index Home li li Html ActionLink About About Home li li Html ActionLink Contact Contact Home li ul nav div div header div id body RenderSection featured required false section class content wrapper main content clear fix RenderBody section div footer div class content wrapper div class float left p copy DateTime Now Year My ASP NET MVC Application p div div footer Scripts Render bundles jquery RenderSection scripts required false body html We have the head section that will be present in all pages head meta charset utf 8 title ViewBag Title My ASP NET MVC Application title link href favicon ico rel shortcut icon type image x icon meta name viewport content width device width Styles Render Content css Scripts Render bundles modernizr head Then you have the header section that will appear in all pages header div class content wrapper div class float left p class site title Html ActionLink your logo here Index Home p div div class float right section id login Html Partial LoginPartial section nav ul id menu li Html ActionLink Home Index Home li li Html ActionLink About About Home li li Html ActionLink Contact Contact Home li ul nav div div header Then you have the header section that will appear in all pages footer div class content wrapper div class float left p copy DateTime Now Year My ASP NET MVC Application p div div footer At one point we see a call to the RenderBody div id body RenderSection featured required false section class content wrapper main content clear fix RenderBody section When the Layout View calls RenderBody that is when the content View index cshtml will be inserted and rendered at this exact point in the HTML markup How does ASP Net MVC to call the Layout View before our content View It is because of this file ViewStart cshtml The contents of the ViewStart cshtml follow Layout Views Shared Layout cshtml ASP Net MVC runtime knows to render first the Layout View because the ViewStart cshtml runs first before any other view and then the Content View 4 Another important topic in ASP Net MVC applications are Partial Views At some point in the Layout cshtml view there is a call to the LoginPartial partial view section id login Html Partial LoginPartial section A partial view is a view that we place code HTML C that we will often reuse in other views Have a look at the contents of the LoginPartial cshtml if Request IsAuthenticated text Hello Html ActionLink User Identity Name Manage Account routeValues null htmlAttributes new class username title Manage using Html BeginForm LogOff Account FormMethod Post new id logoutForm Html AntiForgeryToken a href BLOCKED SCRIPTdocument getElementById logoutForm submit Log off a text else ul li Html ActionLink Register Register Account routeValues null htmlAttributes new id registerLink li li Html ActionLink Log in Login Account routeValues null htmlAttributes new id loginLink li ul So this is a type of special View that we must know and use so we make our application easier to maintain 5 Now it is time to have a look at some important topics that are related with Controllers Open the MovieController cs file I would like to talk a bit about ActionResults and what thet are The ActionResult is an abstract base class for other types of actions Other classes inheriting from the ActionResult ContentResult EmptyResult FileResult HttpStatusCodeResult JavaScriptResult RedirectResult RedirectToRouteResult ViewResult The ViewResult renders a specifed view to the client The RedirectResult performs an HTTP redirection to a specified URL The ContentResult writes content to the client without requiring a view In this method Index the result of this method is ActionResult which means we will call the Index cshtml passing the model to it in the Views Movie folder So the results of this view will be displayed to the client private MovieDBContext db new MovieDBContext GET Movie public ActionResult Index return View db Movies ToList Another very important topic is Action Selectors Some of them are ActionName AcceptVerbs An ActionSelector dictates which action method is triggered They come with the form of attributes Have a look at the MovieController cs file and the Delete method HttpPost ActionName Delete public ActionResult DeleteConfirmed int id Μovie μovie db Movies Find id db Movies Remove μovie db SaveChanges return RedirectToAction Index ActionName will specify the name of this method It can be reached by Delete and not DeleteConfirmed AcceptVerbs HttpPost HttpGet represent attributes that specify which HTTP verbs Get Post an action method will respond to In the example above we have an HTTP Post HttpPost ActionName Delete The final topic I would like to talk about is Action Filters They are applied to methods and classes in the form of attributes An ActionFilter provides some methods that are run before and after request and response processing Some of them are OutputCache which caches the output of the controller Authorize that restricts an action to authorized users or roles If I open the MovieController cs file and then apply the Authorize see below to the Index method private MovieDBContext db new MovieDBContext GET Movie Authorize public ActionResult Index return View db Movies ToList and then navigate to the http localhost 59871 movie this is what I will get the following as a result redirect to the login page The ASP Net MVC runtime will pick up the Authorize attribute and redirect me to the Login page Hope you have followed along and mastered the topics presented here as they are absolutely necessary for building ASP Net MVC applications In the next post we will continue building our application Hope it helps Posted Κυριακή 30 Δεκεμβρίου 2012 11 37 μμ από nikolaosk 0 σχόλια Δημοσίευση στην κατηγορία asp net C CSS Entity framework jquery HTML Code First Visual Studio 2012 ASP NET MVC 4 0 Creating a complete ASP Net MVC 4 0 application with Visual Studio 2012 C EF 5 0 Code First part 3 I have decided to write a series of posts on how to write a small ASP Net MVC 4 0 application I will develop this application step by step and I will explain everything that you need to know in order to develop ASP Net MVC 4 0 applications This is the third post in this series You can find the first one here and the second one here Make sure you read and understand the first post and the second post As I design and develop the application I will explain some of the most common building blocks of ASP Net MVC like Code blocks Code expressions Action Results Action Selectors Action Filters Layout Views and Partial Views Now we have to think about the data access technology that we will use in our sample application I am going to build an ASP Net MVC application where users can search through a collection of movies and rate them I will not use traditional ADO Net data access techniques I will use Entity Framework EF which is part of the Net framework Obviously I cannot go into much detail on what EF is and what it does I will give again a short introduction The Net framework provides support for Object Relational Mapping through EF So EF is a an ORM tool and it is now the main data access technology that microsoft works on I use it quite extensively in my projects Through EF we have many things out of the box provided for us We have the automatic generation of SQL code It maps relational data to strongly types objects All the changes made to the objects in the memory are persisted in a transactional way back to the data store You can search in my blog because I have posted many posts regarding ASP Net and EF There are different approaches paradigms available using the Entity Framework namely Database First Code First Model First You can find in this post an example on how to use the Entity Framework to retrieve data from an SQL Server Database using the Database Schema First approach In this approach we make all the changes at the database level and then we update the model with those changes In this post you can see an example on how to use the Model First approach when working with ASP Net and the Entity Framework This model was firstly introduced in EF version 4 0 and we could start with a blank model and then create a database from that model When we made changes to the model we could recreate the database from the new model The Code First approach is the more code centric than the other two Basically we write POCO classes and then we persist to a database using something called DBContext In this application we will us the Code First approach when building our data centric application with EF Code First relies on DbContext We create 2 3 classes e g Movie Review with properties and then these classes interact with the DbContext class Then we can create a new database based upon our POCOS classes and have tables generated from those classes We do not have an edmx file in this approach By using this approach we can write much easier unit tests DbContext is a new context class and is smaller lightweight wrapper for the main context class which is ObjectContext Schema First and Model First When building an application the most important thing is to understand the domain and the domain business objects These should be the objects that should drive the application and not e g the database schema So I like to design my entities very carefully and 1 Launch Visual Studio and open your application 2 We will add the 2 entities I am going to show you below in the Models folder Add a new class file named Movie cs inside the Models folder The Movie cs entity is as follows public class Μovie public int Id get set public string Name get set public string Director get set public DateTime YearReleased get set public ICollection MovieReview Reviews get set We have added some properties in this entity Now we need to add the MovieReview entity Add a new class file named MovieReview cs inside the Models folder The MovieReview cs entity is as follows public class MovieReview public int Id get set public int Rating get set public string Comment get set public int MovieId get set So we have our entities ready We have one movie and many reviews That should be clear by now I will instantiate these objects store them and retrieve them in my database My database will be an SQL Server database where I will create from the entities Then we need to create a context class that inherits from DbContext Add a new class to the Models folder Name it MovieDBContext cs Now that we have the entity classes created we must let the model know I will have to use the DbSet T property The code for this class follows public class MovieDBContext DbContext public DbSet Μovie Movies get set public DbSet MovieReview Reviews get set The MovieDBContext is a database context class This class is responsible for talking to the underlying database storing and updating the data to the database We need to add this reference to the file using System Data Entity Now we need to create the connection string The only place we can do that is by opening the web config file and adding the following lines of code inside the connectionStrings section connectionStrings add name FootballerDBContext connectionString Data Source LocalDb v11 0 AttachDbFilename DataDirectory MovieReviews mdf Integrated Security True providerName System Data SqlClient connectionStrings 3 Now we need to access our model from a controller This is going to be a simple class that retrieves the footballers data Right click the Controllers folder and create a new MovieController controller Have a look at the

    Original URL path: http://www.dotnetzone.gr/cs/blogs/dotnetrules/archive/tags/CSS/default.aspx (2015-07-15)
    Open archived version from archive



  •