MVC CRUD. Tables: 1) Dinners (First Table)

Similar documents
NerdDinner Step 1: File->New Project

MVC :: Understanding Controllers, Controller Actions, and Action Results

Deploying the ClientDashboard Web Site

MVC 4 Setup and Configuration Training Document. Prepared by Andre Masters. Rev 1.0

Bringing Together One ASP.NET

Bulkmanager User manual

Introducing Models. Data model: represent classes that iteract with a database. Data models are set of

for imis and etouches

SEO Checker User manual

MVC :: Understanding Models, Views, and Controllers

ASP.NET MVC Music Store Tutorial

Foreign-Key Associations

LAMPIRAN. 1. Source Code Data Buku. using System; using System.Collections.Generic; using System.Data; using System.Data.Entity; using System.

Working with Controllers

David Brendel [EVENTCALENDAR DOCUMENTATION] Short documentation for the Umbraco EventCalendar package

SEO Checker User manual

Introduction to using Microsoft Expression Web to build data-aware web applications

1. Prerequisites. Page 1 of 29

5 Years Integrated M.Sc. (IT) 6th Semester Web Development using ASP.NET MVC Practical List 2016

CLARITY WRITE BACK CONFIGURATION White Paper

Certified ASP.NET Programmer VS-1025

Babu Madhav Institute of information technology 2016

In this exercise you will display the Geo-tagged Wikipedia Articles Fusion Table in Google Maps.

MVC - Repository-And-Unit-Of-Work

Technical Manual. Lesotho Health Data Warehouse. Prepared by: Gareth Daniell. Prepared for:

Using Visual Studio 2017

Activating AspxCodeGen 4.0

IPligence and Microsoft SQL how-to guide

Syllabus of Dont net C#

MVC :: Understanding Views, View Data, and HTML Helpers

Roxen Content Provider

.NET Web Applications. Example Project Walk-Through

10267 Introduction to Web Development with Microsoft Visual Studio 2010

Here are the topics covered in this tutorial:

Developing Mobile Apps with Xamarin and Azure

Global Preview v.6.0 for Microsoft Dynamics CRM On-premise 2013, 2015 and 2016

Minimum requirements for Portal (on-premise version):

SAML v2.0 for.net Developer Guide

The connection has timed out

Lab 5: ASP.NET 2.0 Profiles and Localization

Enterprise Architect. User Guide Series. Hybrid Scripting. Author: Sparx Systems. Date: 26/07/2018. Version: 1.0 CREATED WITH

What is SQL? Toolkit for this guide. Learning SQL Using phpmyadmin

PERMISSION REPORTER V4.0

Overview... 7 Tabs... 8 Selected Tables... 8 Load Tables Button Selected Views... 8 Load Views Button Clear Selection Button...

ComponentSpace SAML v2.0 Developer Guide

St. Lucie County School Concurrency Information System

Advanced Programming C# Lecture 5. dr inż. Małgorzata Janik

Tutorial #2: Adding Hyperlinks to ASP.NET

Hands-On Lab. Introduction to SQL Azure. Lab version: Last updated: 12/15/2010

CHAPTER 1: GETTING STARTED WITH ASP.NET 4 1

WIRELESS DATABASE VIEWER PLUS (ENTERPRISE EDITION) SERVER SIDE USER GUIDE

Index. Bower, 133, 352 bower.json file, 376 Bundling files, 157

Exception/Error Handling in ASP.Net

Toolkit Activity Installation and Registration

This tutorial is designed for software programmers who would like to learn the basics of ASP.NET Core from scratch.

Web Database Programming

10267A CS: Developing Web Applications Using Microsoft Visual Studio 2010

Careerarm.com. Question 1. Orders table OrderId int Checked Deptno int Checked Amount int Checked

1.2 - The Effect of Indexes on Queries and Insertions

Course Outline. ASP.NET MVC 5 Development Training Course ASPNETMVC5: 5 days Instructor Led. About this Course

3/1/2016 Copyrights reserved 1

MVC :: Preventing JavaScript Injection Attacks. What is a JavaScript Injection Attack?

DbSchema Forms and Reports Tutorial

OneStop Reporting 4.5 OSR Administration User Guide

Location Framework Degrees.mobi Limited. All rights reserved.

Workspace Administrator Help File

DE Introduction to Web Development with Microsoft Visual Studio 2010

Downloading, Installing, and Configuring Blackboard Drive

SQL Server 2008 Tutorial 3: Database Creation

ASP.NET 2.0 p. 1.NET Framework 2.0 p. 2 ASP.NET 2.0 p. 4 New Features p. 5 Special Folders Make Integration Easier p. 5 Security p.

Getting Started Using HBase in Microsoft Azure HDInsight

SportsStore: Administration

CST272 Getting Started Page 1

Do this by creating on the m: drive (Accessed via start menu link Computer [The m: drive has your login id as name]) the subdirectory CI101.

Developing and deploying MVC4/HTML5 SDK application to IIS DigitalOfficePro Inc.

MS Addons Smart Open. User Guide Smart Open

Creating Rules in Office 365

STORE LOCATOR For Magento 2

Introduction to Web Development with Microsoft Visual Studio 2010

Hands-On Lab. Introduction to SQL Azure. Lab version: Last updated: 11/16/2010

Document Template Authoring

Microsoft Dynamics CRM 2011

How to Use ADFS to Implement Single Sign-On for an ASP.NET MVC Application

Quick Start Guide. This guide will help you get started with Kentico CMS for ASP.NET. It answers these questions:

10264A CS: Developing Web Applications with Microsoft Visual Studio 2010

14FC Works Geotagging Mobile app Telangana Guidelines

Asp Net Mvc Framework Unleashed

Hello, and welcome to the Alexicomtech tutorial. I will show you step by step how to set up your interactive pages. Please feel free to ask questions

Search Hit Report Manual

Embedded101 Blog User Guide

Microsoft ASP.NET Using Visual Basic 2008: Volume 1 Table of Contents

integreat4tfs Installation Guide

ASP.NET Using C# (VS2017)

Advance Dotnet ( 2 Month )

Installation Guide. Last Revision: Oct 03, Page 1-

vfire Server Console Guide Version 1.5

PHPRad. PHPRad At a Glance. This tutorial will show you basic functionalities in PHPRad and

DE-2310 Developing Web Applications Using Microsoft Visual Studio 2008 SP1

Export SharePoint Sites

SHAREPOINT 2013 DEVELOPMENT

Transcription:

Tables: First create one database and name it NerdDinner. Now run following code to generate tables or create by your own if you know how to create and give relationship between two tables. 1) Dinners (First Table) CREATE TABLE [dbo].[dinners]( [DinnerID] [int] IDENTITY(1,1) NOT NULL, [Title] [nvarchar](50) NOT NULL, [EventDate] [datetime] NOT NULL, [Description] [nvarchar](256) NOT NULL, [HostedBy] [nvarchar](20) NOT NULL, [ContactPhone] [nvarchar](20) NOT NULL, [Address] [nvarchar](50) NOT NULL, [Country] [nvarchar](30) NOT NULL, [Latitude] [float] NOT NULL, [Longitude] [float] NOT NULL, CONSTRAINT [PK_Dinners] PRIMARY KEY CLUSTERED ( [DinnerID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] Press F5 after paste above code in sql server for run code.

Create MVC Application and do CRUD: (CREATE, READ, UPDATE, DELETE) Create New MVC Application: File -> New Project Expand Visual C# node -> select web node Choose the ASP.NET MVC 3 Web Application Now give name of the application. Now choose internet application from three options. Click on unit test project check box if you want to test your application using unit test. After clicking ok it will create your mvc application. Just see in your solution explorer there are following basic files that has been automatically created: Now you can run your application by F5 or Ctrl+F5 key and your basic auto configuration file set application will run.

Create Model classes for Database purpose: Right Click on Models folder -> choose Add -> Select New Item just go to data category and Select the ADO.NET Entity Data Model. Click generate from database because we have already created our database. Next-> Click New Connection -> Enter your login Engine Name of your SQL Server into server name and choose your database from connect to database panel. Do test connection and OK. Now you see in Entity Data Model Wizard above footer there is an entity name NerdDinnerEntities which we will use it to interact with the database in our code.

Next-> Choose you tables from the list and Finish. Here is your tables showing in the NerdDinner.edmx save it and close it. Now you see in your Models folder in solution explorer there is a NerdDinner.edmx in that there is a file called NerdDinner.tt in that there is a file Dinner.cs and RSVP.cs created automatically with the get set property of the field names of your tables. Now we will create a new DinnerRepository class for the insert update delete and read queries to manage the code. Right Click on Models folder -> choose Add -> Select New Item-> Select Code left side of your popup dialog box and select class and name it to DinnerRepository. Write following code into DinnerRepository class file. private NerdDinnerEntities entities = new NerdDinnerEntities(); public IQueryable<Dinner> FindAllDinners() return entities.dinners; public IQueryable<Dinner> FindUpcomingDinners() return from dinner in entities.dinners where dinner.eventdate > DateTime.Now orderby dinner.eventdate select dinner; public Dinner GetDinner(int id) return entities.dinners.firstordefault(d => d.dinnerid == id); public void Add(Dinner dinner) entities.dinners.add(dinner); public void Delete(Dinner dinner) entities.dinners.remove(dinner); public void Save()

entities.savechanges();

Create Controller: Now we create controller for handle URL request. Right Click on Controller in Solution Explorer -> Add -> Controller Name it as DinnersController and select template Empty Controller and Add. Now we create action methods which is basically use for call the methods after controller call in URL. We create two methods by which we can communicate with the database and get values as a list and as per the id vise. Write following methods in DinnersController file: DinnerRepository repository = new DinnerRepository(); public ActionResult Index() var dinners = repository.findupcomingdinners().tolist(); return View("Index",dinners); public ActionResult Details(int id) Dinner dinner = repository.getdinner(id); if (dinner == null) return View("NotFound"); else return View("Details",dinner); Right click on NotFound and click on Add View it will create one view of NotFound and write following text in that between <h2></h2> tag. Sorry - but the dinner you requested doesn't exist or was deleted. Now we are going to display our details of Dinners table. Right click on Details of View in Details Action Method in Dinners Controller and select add View. Now->Click Create a strongly-typed view Choose your model file from view data class dropdownlist. Now choose view content as per requirement now we choose details for display our details as detail vise. Click Add.

Run application write http://localhost:portno/dinners/details/1 (1 or any id you have in your database Dinners table) Create Index view template: Create index now which is basically use for display whole table or all data. Right Click on Index which is in view function under Index ActionMethod. Click on Create a strongly typed View. Now choose your model file (for example: for us it is Dinner (NerdDinner.Models)) like wise. Now click on Add button and system will automatically create the view having table and fill data which are in dinner table having name Index in Views/Dinners/Index.cshtml in solution explorer. Now Run your application after debug. It will look like below picture: NOTE: If your application not running properly,you have no data in table or your date for event is not in a week go with the processes below and after create function add data and run this again. Now you see in Index view in the last line: @Html.ActionLink("Edit", "Edit", new id=item.dinnerid ) @Html.ActionLink("Details", "Details", new id=item.dinnerid ) @Html.ActionLink("Delete", "Delete", new id=item.dinnerid ) is basically give us a link of particular data so we can get more detail by clicking on that data like in gridview. Detail or view or edit it or delete it.

Now We create ActionMethods and Views for CRUD: Let s do Create Update Delete: Create: Now we do Create action method for create for add new data in database. Write following code of actionmethod in dinnerscontroller after Details actionmethod: public ActionResult Create() Dinner dinner = new Dinner() EventDate = DateTime.Now.AddDays(7) ; return View(dinner); Now we create view for Create ActionMethod: So Right click on Create function method name and click on add view and click on create strongly type and select your dinner model file from model class and choose Create in scaffold template and click ok. Now run your code in browser and write your url for example: http://localhost:49161/dinners/create And fill the form with new details and click on create button to save data but you are not able to save data in database now so for that you have to create same method for saving the data in database. Now create one new method like below to take your all data from form and save into database. [HttpPost] public ActionResult Create(FormCollection formvalues) Dinner dinner = new Dinner(); if(tryupdatemodel(dinner)) repository.add(dinner); repository.save(); return RedirectToAction("Details", new id=dinner.dinnerid); return View(dinner);

Now if you remember you have created repository file at starting of this tutorial that contains different different methods for add data, delete data and details etc etc So you are using now those methods in above actionmethod for CRUD in MVC. Like: (repository.add(dinner)) There is one line where you see [HttpPost] which is basically use for sending the data as a post method for saving in database. Now you run your application and just write /Dinners/Create after http:localhost:49161 url and fill the form and click on Create button: http://localhost:49161/dinners/create You see your data filled successfully and your filled information display in a page with url Details.

Update: Now we create edit action method for get details from database and fill it in a textboxes so we can do update process. Write following code of actionmethod in dinnerscontroller after create actionmethod which is [httppost]: public ActionResult Edit(int id) Dinner dinner = repository.getdinner(id); return View(dinner); Now we create view for edit: So Right click on Edit function method name and click on add view and click on create strongly type and select your dinner model file from model class and choose edit in scaffold template and click ok. Now run your code in browser and write your url for example: http://localhost:49161/dinners/edit/1 (1 is my database id you have to write your id which is available in your database table) Still you just get the details as per your id passed but you cannot update so for update we create the same method for edit as we done in create process. So write down following code in your application after Edit ActionMethod: [HttpPost] public ActionResult Edit(int id, FormCollection formvalues) Dinner dinner = repository.getdinner(id); UpdateModel(dinner); if (TryUpdateModel(dinner)) repository.save(); return RedirectToAction("Details", new id = dinner.dinnerid ); return View(dinner); Here you done the code for update information that you want to update.

There is a dinner object of a table and repository object by which you use it for use methods that you created in repository file. repository.save(); is used for saving details that you updated and after update done you have to display those data that you update so there is a RedirectToAction which is basially use like response.redirect it will display data that you update. ("Details", new id = dinner.dinnerid ); that contains the view name and id that you want to display the detail. Now run your application like below: http://localhost:49161/dinners/edit/1 (1 is my database id you have to write your id which is available in your database) and you see that your passed id s all information filled in textboxes so just change values those you want to change and click on Save button.

Delete: For Delete you have to first get those data that you want to delete and than you can delete particular data: So for that write following actionmethod first: public ActionResult Delete(int id) Dinner dinner = repository.getdinner(id); if (dinner == null) return View("NotFound"); else return View(dinner); Now we create view for Delete: So Right click on Delete function method name and click on add view and click on create strongly type and select your dinner model file from model class and choose Delete in scaffold template and click ok. Now run your code in browser and write your url for example: http://localhost:49161/dinners/delete/1 Here you just displaying the data which user want to delete and ask that person that are you sure want to delete so after clicking delete here your data must be delete so for that we have to create Delete method again as a HttpPost. So write following code for delete after confirmation: [HttpPost] public ActionResult Delete(int id, string confirmbutton) Dinner dinner = repository.getdinner(id); if (dinner == null) return View("NotFound"); repository.delete(dinner); repository.save(); return View("Deleted"); So if you run your application like http://localhost:49161/dinners/delete/1 and it will display data that you want to delete with confirmation that sure you want to delete? And now when you click on delete button it will delete your data from

database and if you choose Back to List than it will redirect you to the index page of your application. So that s it for CRUD in MVC for more details send your feedback or query on: Or go on site for more study: dotnetexpertteam.wordpress.com