Sunteți pe pagina 1din 70

1

MVC CRUD using .NET SDK

Couchbase ASP.NET MVC


This tutorial walks you through the steps of creating an ASP.NET MVC app using Couchbase. It will focus on practical
design patterns, best practices along with general SDK usage.

Prerequisites
This tutorial assumes that you have Visual Studio 2010 installed, along with ASP.NET MVC 2/4. You may use any edition
of Visual Studio or you may use Visual Web Developer. Visual Studio Web Developer 2010 Express will also work for this
tutorial, but the screenshots included will be from Visual Studio 2012 Professional.
You will also need to have an installation of Couchbase Server 2.0 and have obtained the latest Couchbase .NET Client
Library, version 1.3 or higher.
You also may use an older version of ASP.NET MVC if you do not have MVC 4 installed, but as with using Visual Web
Developer or Visual Studio 2012, the templates shown in the screenshots will vary from what you see.
You should also have installed the beer-sample database on your Couchbase Server.

Visual Studio Project Setup


This project will be based on an ASP.NET MVC 2 application template. After opening Visual Studio, select File -> New
Project and then select Web -> ASP.NET MVC 2 Application under the Visual C# project templates. Name the project
CouchbaseBeersWeb and click OK to create the solution.

hPot-Tech

MVC CRUD using .NET SDK

[Start with an Empty application using the Razor view engine for the MVC template.] optional

hPot-Tech

MVC CRUD using .NET SDK

Next youll need to add a reference to the Couchbase .NET Client Library.Add references to Couchbase.dll,
Enyim.Caching.dll and the dependencies Newtonsoft.Json.dll and Hammock.dll. These assemblies are found in the zip
file and should be referenced in your project.

hPot-Tech

MVC CRUD using .NET SDK

Working with Models


The first task to solve is displaying a list of breweries in from our beer-sample bucket. To add this functionality, there is
some plumbing to setup in our application. These tasks are enumerated below.

Create a Brewery model class to represent beer documents

Create a BreweryRepository to encapsulate data access for Brewery instances

Create a BreweriesController with an Index action used to show a Brewery list

Create a [Razor] view to display our list of breweries

As a JSON document database, Couchbase supports a natural mapping of domain objects to data items. In other words,
theres very little difference between the representation of your data as a class in C# and the representation of your data
as a document in Couchbase. Your object becomes the schema defined in the JSON document.
When working with domain objects that will map to documents in Couchbase, its useful, but not required, to define a base
class from which your model classes will derive. This base class will be abstract and contain two properties, Id and
Type.

hPot-Tech

MVC CRUD using .NET SDK

Right click on the Models directory and add a new class named ModelBase and include the following code.
public abstract class ModelBase
{
public virtual string Id { get; set; }
public abstract string Type { get; }
}

Note that the Type method is abstract and readonly. It will be implemented by subclasses simply by returning a hardcoded string, typically matching the class name, lower-cased. The purpose of the Type property is to provide taxonomy to
the JSON documents stored in your Couchbase bucket.
Next, create a new class namedin the Models directory of your project. This class will be a plain old CLR object (POCO)
that simply has properties mapping to the properties of brewery documents in the beer-sample bucket. It will also
extend ModelBase .
public class Brewery : ModelBase
{
public string Name { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Code { get; set; }
public string Country { get; set; }

hPot-Tech

MVC CRUD using .NET SDK

public string Phone { get; set; }


public string Website { get; set; }
public DateTime Updated { get; set; }
public string Description { get; set; }
public IList<string> Addresses { get; set; }
public IDictionary<string, object> Geo { get; set; }
public override string Type
{
get { return "brewery"; }
}
}
}

hPot-Tech

MVC CRUD using .NET SDK

Sample Document
{
"name": "Thomas Hooker Brewing",
"city": "Bloomfield",
"state": "Connecticut",
"code": "6002",
"country": "United States",
"phone": "860-242-3111",
"website": "http://www.hookerbeer.com/",
"type": "brewery",
"updated": "2010-07-22 20:00:20",
"description": "Tastings every Saturday from 12-6pm, and 1st and 3rd Friday of every month from 5-8.",
"address": [
"16 Tobey Road"
],
"geo": {
"accuracy": "RANGE_INTERPOLATED",
"lat": 41.8087,

hPot-Tech

MVC CRUD using .NET SDK

"lng": -72.7108
}
}

Encapsulating Data Access


After creating the Brewery class, the next step is to create the data access classes that will encapsulate our Couchbase
CRUD and View operations. Create a new file in Models named RepositoryBase.cs with a class name of
RepositoryBase. This will be an abstract class, generically constrained to work with ModelBase instances.
public abstract class RepositoryBase<T> where T : ModelBase
{
//CRUD methods
}

The process of creating an instance of a CouchbaseClient is expensive. There is a fair amount of overhead as the client
establishes connections to the cluster. It is therefore recommended to minimize the number of times that a client instance
is created in your application. The simplest approach is to create a static property or singleton that may be accessed from
data access code. Using the RepositoryBase , setting up a protected static property will provide access for subclasses.
public abstract class RepositoryBase<T> where T : ModelBase
{
protected static CouchbaseClient _Client { get; set; }
static RepositoryBase()

hPot-Tech

MVC CRUD using .NET SDK

{
_Client = new CouchbaseClient();
}
}

The code above requires setting configuration in web.config.


<configSections>
<section name="couchbase" type="Couchbase.Configuration.CouchbaseClientSection, Couchbase"/>
</configSections>
<couchbase>
<servers bucket="beer-sample">
<add uri="http://127.0.0.1:8091/pools"/>
</servers>
</couchbase>

hPot-Tech

10

MVC CRUD using .NET SDK

Working with Views


To display a list of all breweries, a view will be necessary. This map function for this view will simply emit null keys and
values for each of the brewery documents in the database. This view will live in a breweries design document and be
named all.
function(doc, meta) {
if (doc.type == "brewery") {
emit(null, null);
}
}

hPot-Tech

11

MVC CRUD using .NET SDK

hPot-Tech

12

MVC CRUD using .NET SDK

If you click the link next to the Filter Results button, you will see the JSON that is returned to the CouchbaseClient when
querying a view. Notice the id property found in each row. That is the key that was used to store the document.
{"total_rows":1412,"rows":[
{"id":"21st_amendment_brewery_cafe","key":null,"value":null},
{"id":"357","key":null,"value":null},
{"id":"3_fonteinen_brouwerij_ambachtelijke_geuzestekerij","key":null,"value":null},
{"id":"512_brewing_company","key":null,"value":null},
{"id":"aass_brewery","key":null,"value":null},
{"id":"abbaye_de_leffe","key":null,"value":null},
{"id":"abbaye_de_maredsous","key":null,"value":null},
{"id":"abbaye_notre_dame_du_st_remy","key":null,"value":null},
{"id":"abbey_wright_brewing_valley_inn","key":null,"value":null},
{"id":"aberdeen_brewing","key":null,"value":null}
]
}

hPot-Tech

13

MVC CRUD using .NET SDK

With the view created, the next step is to modify the RepositoryBase to have a GetAll method.
The initial implementation of GetAll will simply return all breweries using the generic GetView<T> method
of CouchbaseClient . The third parameter instructs CouchbaseClient to retrieve the original document rather than
deserialize the value of the view row.
public virtual IEnumerable<T> GetAll(int limit = 0)
{
var view = _Client.GetView<T>("dev_breweries", "all", true).Stale(StaleMode.False);
if (limit > 0) view.Limit(limit);
return view;
}

RepositoryBase is a generic and abstract class, so obviously it cannot be used directly. Create a new class in Models

named BreweryRepository. The code for this class is very minimal, as it will rely on its base class for most functionality.
public class BreweryRepository : RepositoryBase<Brewery>
{
}

hPot-Tech

14

MVC CRUD using .NET SDK

The Views and Controller


With the models and repository coded, the next step is to create the controller. Right click on the Controllers directory in
the project and select Add -> Controller. Name the controller BreweriesController and select the template Controller with
empty read/write actions, which will create actions for creating, updating, deleting, showing and listing breweries.

hPot-Tech

15

MVC CRUD using .NET SDK

Or

The Index method of the BreweriesController will be used to display the list of breweries. To allow the new controller to
access brewery data, it will need an instance of a BreweryRepository . Create a public property of
type BreweryRepository and instantiate it in the default constructor.
public BreweryRepository BreweryRepository { get; set; }
public BreweriesController()
{
BreweryRepository = new BreweryRepository();
}

Then inside of the Index method, add a call to BreweryRepository s GetAll method and pass its results to the view as
its model.

hPot-Tech

16

MVC CRUD using .NET SDK

public ActionResult Index()


{
var breweries = BreweryRepository.GetAll(20);
return View(breweries);
}

The last step to displaying the list of breweries is to create the [Razor] view (as in MVC views, not Couchbase views). In
the Views directory, create a new directory named Breweries. Right click on that new directory and select Add ->
View. Name the view Index and create it as a strongly typed (to the Brewery class) view with List scaffolding. This
template will create a Razor view that loops over the brewery results, displaying each as a row in an HTML table.

hPot-Tech

17

MVC CRUD using .NET SDK

hPot-Tech

18

MVC CRUD using .NET SDK

At this point, you should build your application and navigate to the Breweries path [ctrl + F5]
If all went well, you should see a list of breweries.
http://localhost:3854/Breweries

hPot-Tech

19

MVC CRUD using .NET SDK

Brewery CRUD
The MVC scaffolding that created the Razor template to list breweries also included links to create, show, edit and delete
breweries. Using more scaffolding, these CRUD features are easily implemented.
Add the following BuildKey method to the RepositoryBase to allow for default key creation based on the Id property.
protected virtual string BuildKey(T model)
{
if (string.IsNullOrEmpty(model.Id))
{
return Guid.NewGuid().ToString();
}
return

return model.Id.ToLower();

BuildKey will default to a GUID string when no Id is provided. Its also virtual so that subclasses are able to override the

default behavior.

hPot-Tech

20

MVC CRUD using .NET SDK

When storing a Brewery instance in Couchbase Server, it first has to be serialized into a JSON string. An important
consideration is how to map the properties of the Brewery to properties of the JSON document.
JSON.NET (from Newtonsoft.Json) will by default serialize all properties. However, ModelBase objects all have an Id
property that shouldnt be serialized into the stored JSON. That Id is already being used as the documents key (in the
key/value operations), so it would be redundant to store it in the JSON.
JSON.NET supports various serialization settings, including which properties should be included in serialization.
In RepositoryBase , create a serializAndIgnoreId method and a private DocumentIdContractResolver class as shown
below.
private string serializeAndIgnoreId(T obj)
{
var json = JsonConvert.SerializeObject(obj,
new JsonSerializerSettings()
{
ContractResolver = new DocumentIdContractResolver(),
});
return json;
}

private class DocumentIdContractResolver : CamelCasePropertyNamesContractResolver


{

hPot-Tech

21

MVC CRUD using .NET SDK

protected override List<MemberInfo> GetSerializableMembers(Type objectType)


{
return base.GetSerializableMembers(objectType).Where(o => o.Name != "Id").ToList();
}
}

The DocumentIdContractResolver will prevent the Id property from being saved into the JSON. It also
extends CamelCasePropertyNamesContractResolver to provide camel-cased properties in the JSON output.

With this new plumbing in place, its now possible to complete the Create , Update and Save methods. (RepositoryBase)

public virtual int Create(T value, PersistTo persistTo = PersistTo.Zero)


{
var result = _Client.ExecuteStore(StoreMode.Add, BuildKey(value), serializeAndIgnoreId(value), persistTo);
if (result.Exception != null) throw result.Exception;
return result.StatusCode.Value;
}

hPot-Tech

22

MVC CRUD using .NET SDK

public virtual int Update(T value, PersistTo persistTo = PersistTo.Zero)


{
var result = _Client.ExecuteStore(StoreMode.Replace, value.Id, serializeAndIgnoreId(value), persistTo);
if (result.Exception != null) throw result.Exception;
return result.StatusCode.Value;
}

Get will return the object or null if not found, while throwing a swallowed exception.
public virtual T Get(string key)
{
var result = _Client.ExecuteGet<string>(key);
if (result.Exception != null) throw result.Exception;

if (result.Value == null)
{
return null;
}

hPot-Tech

23

MVC CRUD using .NET SDK

var model = JsonConvert.DeserializeObject<T>(result.Value);


model.Id = key; //Id is not serialized into the JSON document on store, so need to set it before returning
return model;
}

Brewery Forms
With the new methods implemented, its time to create the scaffolding for the CRUD forms. The first task will be to create
an edit form. Open the BreweriesController and locate the Edit methods that were generated by the Add Controller
wizard.
In the HTTP GET override of Edit , modify it as shown below. This action will retrieve the Brewery and pass it to the view
as the model. Note the change from an int id parameter to a string id parameter.
public ActionResult Edit(string id)
{
var brewery = BreweryRepository.Get(id);
return View(brewery);
}

hPot-Tech

24

MVC CRUD using .NET SDK

Update the Edit method that handles POSTs as shown below. Validation and error handling are intentionally being
omitted for brevity.
[HttpPost]
public ActionResult Edit(string id, Brewery brewery)
{
try
{
// TODO: Add update logic here
BreweryRepository.Update(brewery);
return RedirectToAction("Index");
}
catch
{
return View();
}}

The edit form will be created using scaffolding, as was the case with the listing page. Right click on the Breweries folder
in the Views directory and click Add -> View. Name the view Edit and strongly type it to a Brewery with Edit scaffolding.

hPot-Tech

25

MVC CRUD using .NET SDK

Rebuild the application and return to the brewery listing page. Click on an Edit link and you should see the edit form
loaded with the details for that brewery. Edit some values on the form and click save. You should see your changes
persisted on the listing page.

hPot-Tech

26

MVC CRUD using .NET SDK

hPot-Tech

27

MVC CRUD using .NET SDK

The Details action looks very much like Edit . Get the Brewery and provide it as the model for the view.
public ActionResult Details(string id)
{
var brewery = BreweryRepository.Get(id);
return View(brewery);
}

Create a scaffolding form for Details using the same process as was used with Edit .

hPot-Tech

28

MVC CRUD using .NET SDK

Rebuild and return to the list page. Click on a Details link. You should see a page listing the data for that brewery.

hPot-Tech

29

MVC CRUD using .NET SDK

hPot-Tech

30

MVC CRUD using .NET SDK

The Create and Edit actions of the BreweriesController are quite similar, save for the fact that Creates GET method
doesnt provide a model to the view. Again, error handling and validation are being omitted for brevitys sake.
public ActionResult Create()
{
return View();
}
[HttpPost]
public ActionResult Create(Brewery brewery)
{
try
{
// TODO: Add insert logic here
BreweryRepository.Create(brewery);
return RedirectToAction("Index");
}
catch
{
return View();
}
}

Go through the scaffolding process again to add a create view for the Create action. Rebuild and click the Create New
link on the list page to test the new form. Breweries (for now) are sorted by key and limited to 20, so you might not see
yours in the list. If you want to verify your create action worked, use brewery name that starts with a numeric value (e.g.,
123 Brewery).
Also now (because of the stale setting), if you create a new Brewery, it should appear after a redirect and should not
require a refresh.

hPot-Tech

31

MVC CRUD using .NET SDK

The last piece required to complete the CRUD functionality for breweries is to implement the delete
form. Update the Delete actions in BreweriesController as shown below.
public ActionResult Delete(string id)
{
var brewery = BreweryRepository.Get(id);
return View(brewery);
}
[HttpPost]
public ActionResult Delete(string id, Brewery brewery)
{
try
{
// TODO: Add delete logic here
BreweryRepository.Delete(id);
return RedirectToAction("Index");
}
catch
{
return View();
}}

To create the delete form, simply go through the Add View process again and choose scaffolding for delete
(dont forget to choose the Brewery model).

hPot-Tech

32

MVC CRUD using .NET SDK

Test : Run Ctrl + F5


http://localhost:3854/Breweries

Click on Edit

hPot-Tech

33

MVC CRUD using .NET SDK

Update City:

Save

hPot-Tech

34

MVC CRUD using .NET SDK

Click on details:

hPot-Tech

35

MVC CRUD using .NET SDK

Click on Back To List  Delete

hPot-Tech

36

MVC CRUD using .NET SDK

hPot-Tech

37

MVC CRUD using .NET SDK

Scroll to last page and click on Create

Verify on the console by ID

hPot-Tech

38

MVC CRUD using .NET SDK

Congrats!

hPot-Tech

39

MVC CRUD using .NET SDK

Source Code Complete:

Add references as above

hPot-Tech

40

MVC CRUD using .NET SDK

Controller:
using
using
using
using
using
using

System;
System.Collections.Generic;
System.Linq;
System.Web;
System.Web.Mvc;
CouchbaseBeersWeb.Models;

namespace CouchbaseBeersWeb.Controllers
{
public class BreweriesController : Controller
{
public BreweryRepository BreweryRepository { get; set; }
public BreweriesController()
{
BreweryRepository = new BreweryRepository();
}
//
// GET: /Breweries/
public ActionResult Index()
{
var breweries = BreweryRepository.GetAll(50);
return View(breweries);
}
//
// GET: /Breweries/Details/5
public ActionResult Details(string id)
{
hPot-Tech

41

MVC CRUD using .NET SDK

var brewery = BreweryRepository.Get(id);


return View(brewery);
}
//
// GET: /Breweries/Create
public ActionResult Create()
{
return View();
}
//
// POST: /Breweries/Create
[HttpPost]
public ActionResult Create(Brewery brewery)
{
try
{
// TODO: Add insert logic here
BreweryRepository.Create(brewery);
return RedirectToAction("Index");
}
catch
{
return View();
}
}
//
// GET: /Breweries/Edit/5

hPot-Tech

42

MVC CRUD using .NET SDK

public ActionResult Edit(string id)


{
var brewery = BreweryRepository.Get(id);
return View(brewery);
}
//
// POST: /Breweries/Edit/5
[HttpPost]
public ActionResult Edit(string id, Brewery brewery)
{
try
{
// TODO: Add update logic here
BreweryRepository.Update(brewery);
return RedirectToAction("Index");
}
catch
{
return View();
}
}
//
// GET: /Breweries/Delete/5
public ActionResult Delete(String id)
{
var brewery = BreweryRepository.Get(id);
return View(brewery);
}

hPot-Tech

43

MVC CRUD using .NET SDK

//
// POST: /Breweries/Delete/5
[HttpPost]
public ActionResult Delete(string id, Brewery brewery)
{
try
{
// TODO: Add delete logic here
BreweryRepository.Delete(id);
return RedirectToAction("Index");
}
catch
{
return View();
}
}
}
}

hPot-Tech

44

MVC CRUD using .NET SDK

Models:
using
using
using
using

System;
System.Collections.Generic;
System.Linq;
System.Web;

namespace CouchbaseBeersWeb.Models
{
public class Brewery : ModelBase
{
public string Name { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Code { get; set; }
public string Country { get; set; }
public string Phone { get; set; }
public string Website { get; set; }
public DateTime Updated { get; set; }
public string Description { get; set; }
public IList<string> Addresses { get; set; }
public IDictionary<string, object> Geo { get; set; }
public override string Type
{
get { return "brewery"; }
}
}
}

hPot-Tech

45

MVC CRUD using .NET SDK

using
using
using
using
using
using

System;
System.Collections.Generic;
System.Linq;
System.Web;
Couchbase;
Couchbase.Configuration;

using
using
using
using
using
using

Enyim.Caching.Memcached.Results;
Enyim.Caching.Memcached;
Newtonsoft.Json;
Newtonsoft.Json.Serialization;
System.Reflection;
Couchbase.Operations;

namespace CouchbaseBeersWeb.Models
{
public class BreweryRepository : RepositoryBase<Brewery>
{
/// <summary>
/// Create a Brewery, blocking until persisted to master node
/// Views will not consider a new record for an index
/// until persisted to disk.
/// </summary>
/// <param name="value">Brewery to create</param>
/// <returns>Status code (0 on success)</returns>
public int Create(Brewery brewery)
{
return base.Create(brewery, PersistTo.One);
}
/// <summary>
hPot-Tech

46

MVC CRUD using .NET SDK

/// Remove a Brewery, blocking until removed from disk


/// Views will not remove a deleted record from an index
/// until removed from disk.
/// </summary>
/// <param name="key">Key of brewery to delete</param>
/// <returns>Status code (0 on success)</returns>
public int Delete(string key)
{
return base.Delete(key, PersistTo.One);
}
}
}

hPot-Tech

47

MVC CRUD using .NET SDK

using
using
using
using

System;
System.Collections.Generic;
System.Linq;
System.Web;

namespace CouchbaseBeersWeb.Models
{
public abstract class ModelBase
{
public virtual string Id { get; set; }
public abstract string Type { get; }
}
}

hPot-Tech

48

MVC CRUD using .NET SDK

using
using
using
using
using
using
using
using
using
using
using
using
using

System;
System.Collections.Generic;
System.Linq;
System.Web;
Couchbase;
Couchbase.Configuration;
Enyim.Caching.Memcached;
Newtonsoft.Json;
Enyim.Caching.Memcached.Results;
Couchbase.Extensions;
Couchbase.Operations;
Newtonsoft.Json.Serialization;
System.Reflection;

namespace CouchbaseBeersWeb.Models
{
public abstract class RepositoryBase<T> where T : ModelBase
{
protected static CouchbaseClient _Client { get; set; }
static RepositoryBase()
{
_Client = new CouchbaseClient();
}
/// <summary>
/// Retrieve a document from a bucket
/// </summary>
/// <param name="key">Key of document</param>
/// <returns>When key is found, returns document, else null.</returns>
public virtual T Get(string key)
{
var result = _Client.ExecuteGet<string>(key);
if (result.Exception != null) throw result.Exception;

hPot-Tech

49

MVC CRUD using .NET SDK

if (result.Value == null)
{
return null;
}
var model = JsonConvert.DeserializeObject<T>(result.Value);
model.Id = key; //Id is not serialized into the JSON document on store, so need to set
it before returning
return model;
}
public virtual IEnumerable<T> GetAll(int limit = 0)
{
var view = _Client.GetView<T>("dev_breweries", "all", true).Stale(StaleMode.False);
if (limit > 0) view.Limit(limit);
return view;
}
public virtual int Create(T value, PersistTo persistTo = PersistTo.Zero)
{
var result = _Client.ExecuteStore(StoreMode.Add, BuildKey(value),
serializeAndIgnoreId(value), persistTo);
if (result.Exception != null) throw result.Exception;
return result.StatusCode.Value;
}

public virtual int Update(T value, PersistTo persistTo = PersistTo.Zero)


{
var result = _Client.ExecuteStore(StoreMode.Replace, value.Id,
serializeAndIgnoreId(value), persistTo);
if (result.Exception != null) throw result.Exception;
return result.StatusCode.Value;
}
hPot-Tech

50

MVC CRUD using .NET SDK

private string serializeAndIgnoreId(T obj)


{
var json = JsonConvert.SerializeObject(obj,
new JsonSerializerSettings()
{
ContractResolver = new DocumentIdContractResolver(),
});
return json;
}
/// <summary>
/// ContractResolver implementation used to instruct Json.NET not to serialize
/// the Id property on models, since it is already part of the meta data available
/// to views. Extends CamelCasePropertyNamesContractResolver to provide standard
/// JSON property naming conventions.
/// </summary>
private class DocumentIdContractResolver : CamelCasePropertyNamesContractResolver
{
protected override List<MemberInfo> GetSerializableMembers(Type objectType)
{
return base.GetSerializableMembers(objectType).Where(o => o.Name !=
"Id").ToList();
}
}
///
///
///
///
///
///
///

<summary>
Default key generation strategy. If no key is provided
then GUID is created. Subclasses should override
when Id property shouldn't be used.
</summary>
<param name="model"></param>
<returns></returns>
hPot-Tech

51

MVC CRUD using .NET SDK

protected virtual string BuildKey(T model)


{
if (string.IsNullOrEmpty(model.Id))
{
return Guid.NewGuid().ToString();
}
return model.Id.ToLower();
}
/// <summary>
/// Delete a key.
/// </summary>
/// <param name="key">Key of document to delete</param>
/// <param name="persistTo">Durability requirement</param>
/// <returns>Status code of Remove operation</returns>
public virtual int Delete(string key, PersistTo persistTo = PersistTo.Zero)
{
var result = _Client.ExecuteRemove(key, persistTo);
if (result.Exception != null) throw result.Exception;
return result.StatusCode.HasValue ? result.StatusCode.Value : 0;
}
}
}

hPot-Tech

52

MVC CRUD using .NET SDK

Views:
Create.aspx:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage<CouchbaseBeersWeb.Models.Brewery>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Create
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Create</h2>
<% using (Html.BeginForm()) {%>
<%: Html.ValidationSummary(true) %>
<fieldset>
<legend>Fields</legend>
<div class="editor-label">
<%: Html.LabelFor(model => model.Name) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Name) %>
<%: Html.ValidationMessageFor(model => model.Name) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.City) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.City) %>
<%: Html.ValidationMessageFor(model => model.City) %>
</div>
hPot-Tech

53

MVC CRUD using .NET SDK

<div class="editor-label">
<%: Html.LabelFor(model => model.State) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.State) %>
<%: Html.ValidationMessageFor(model => model.State) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Code) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Code) %>
<%: Html.ValidationMessageFor(model => model.Code) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Country) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Country) %>
<%: Html.ValidationMessageFor(model => model.Country) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Phone) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Phone) %>
<%: Html.ValidationMessageFor(model => model.Phone) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Website) %>
</div>
hPot-Tech

54

MVC CRUD using .NET SDK

<div class="editor-field">
<%: Html.TextBoxFor(model => model.Website) %>
<%: Html.ValidationMessageFor(model => model.Website) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Updated) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Updated) %> (7/22/2010 8:00 PM)
<%: Html.ValidationMessageFor(model => model.Updated) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Description) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Description) %>
<%: Html.ValidationMessageFor(model => model.Description) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Id) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Id) %>
<%: Html.ValidationMessageFor(model => model.Id) %>
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
<% } %>

hPot-Tech

55

MVC CRUD using .NET SDK

<div>
<%: Html.ActionLink("Back to List", "Index") %>
</div>
</asp:Content>

hPot-Tech

56

MVC CRUD using .NET SDK

Delete.aspx:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"


Inherits="System.Web.Mvc.ViewPage<CouchbaseBeersWeb.Models.Brewery>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Delete
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Delete</h2>
<h3>Are you sure you want to delete this?</h3>
<fieldset>
<legend>Fields</legend>
<div class="display-label">Name</div>
<div class="display-field"><%: Model.Name %></div>
<div class="display-label">City</div>
<div class="display-field"><%: Model.City %></div>
<div class="display-label">State</div>
<div class="display-field"><%: Model.State %></div>
<div class="display-label">Code</div>
<div class="display-field"><%: Model.Code %></div>
<div class="display-label">Country</div>
<div class="display-field"><%: Model.Country %></div>
<div class="display-label">Phone</div>
<div class="display-field"><%: Model.Phone %></div>
<div class="display-label">Website</div>
hPot-Tech

57

MVC CRUD using .NET SDK

<div class="display-field"><%: Model.Website %></div>


<div class="display-label">Updated</div>
<div class="display-field"><%: String.Format("{0:g}", Model.Updated) %></div>
<div class="display-label">Description</div>
<div class="display-field"><%: Model.Description %></div>
<div class="display-label">Type</div>
<div class="display-field"><%: Model.Type %></div>
<div class="display-label">Id</div>
<div class="display-field"><%: Model.Id %></div>
</fieldset>
<% using (Html.BeginForm()) { %>
<p>
<input type="submit" value="Delete" /> |
<%: Html.ActionLink("Back to List", "Index") %>
</p>
<% } %>
</asp:Content>

hPot-Tech

58

MVC CRUD using .NET SDK

Details.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"


Inherits="System.Web.Mvc.ViewPage<CouchbaseBeersWeb.Models.Brewery>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Details
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Details</h2>
<fieldset>
<legend>Fields</legend>
<div class="display-label">Name</div>
<div class="display-field"><%: Model.Name %></div>
<div class="display-label">City</div>
<div class="display-field"><%: Model.City %></div>
<div class="display-label">State</div>
<div class="display-field"><%: Model.State %></div>
<div class="display-label">Code</div>
<div class="display-field"><%: Model.Code %></div>
<div class="display-label">Country</div>
<div class="display-field"><%: Model.Country %></div>
<div class="display-label">Phone</div>
<div class="display-field"><%: Model.Phone %></div>
<div class="display-label">Website</div>
<div class="display-field"><%: Model.Website %></div>
hPot-Tech

59

MVC CRUD using .NET SDK

<div class="display-label">Updated</div>
<div class="display-field"><%: String.Format("{0:g}", Model.Updated) %></div>
<div class="display-label">Description</div>
<div class="display-field"><%: Model.Description %></div>
<div class="display-label">Type</div>
<div class="display-field"><%: Model.Type %></div>
<div class="display-label">Id</div>
<div class="display-field"><%: Model.Id %></div>
</fieldset>
<p>
<%: Html.ActionLink("Edit", "Edit", new { id=Model.Id }) %> |
<%: Html.ActionLink("Back to List", "Index") %>
</p>
</asp:Content>

hPot-Tech

60

MVC CRUD using .NET SDK

Edit.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"


Inherits="System.Web.Mvc.ViewPage<CouchbaseBeersWeb.Models.Brewery>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Edit
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Edit</h2>
<% using (Html.BeginForm()) {%>
<%: Html.ValidationSummary(true) %>
<fieldset>
<legend>Fields</legend>
<div class="editor-label">
<%: Html.LabelFor(model => model.Name) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Name) %>
<%: Html.ValidationMessageFor(model => model.Name) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.City) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.City) %>
<%: Html.ValidationMessageFor(model => model.City) %>
</div>
<div class="editor-label">
hPot-Tech

61

MVC CRUD using .NET SDK

<%: Html.LabelFor(model => model.State) %>


</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.State) %>
<%: Html.ValidationMessageFor(model => model.State) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Code) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Code) %>
<%: Html.ValidationMessageFor(model => model.Code) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Country) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Country) %>
<%: Html.ValidationMessageFor(model => model.Country) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Phone) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Phone) %>
<%: Html.ValidationMessageFor(model => model.Phone) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Website) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Website) %>
hPot-Tech

62

MVC CRUD using .NET SDK

<%: Html.ValidationMessageFor(model => model.Website) %>


</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Updated) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Updated) %>
<%: Html.ValidationMessageFor(model => model.Updated) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Description) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Description) %>
<%: Html.ValidationMessageFor(model => model.Description) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Id) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Id) %>
<%: Html.ValidationMessageFor(model => model.Id) %>
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
<% } %>
<div>
<%: Html.ActionLink("Back to List", "Index") %>
hPot-Tech

63

MVC CRUD using .NET SDK

</div>
</asp:Content>

hPot-Tech

64

MVC CRUD using .NET SDK

Index.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"


Inherits="System.Web.Mvc.ViewPage<IEnumerable<CouchbaseBeersWeb.Models.Brewery>>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Index
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Index</h2>
<table>
<tr>
<th></th>
<th>
Name
</th>
<th>
City
</th>
<th>
State
</th>
<th>
Code
</th>
<th>
Country
</th>
<th>
Phone
</th>
<th>
Website
hPot-Tech

65

MVC CRUD using .NET SDK

</th>
<th>
Updated
</th>
<th>
Description
</th>
<th>
Type
</th>
<th>
Id
</th>
</tr>
<% foreach (var item in Model) { %>
<tr>
<td>
<%:
<%:
<%:
</td>
<td>
<%:
</td>
<td>
<%:
</td>
<td>
<%:
</td>
<td>
<%:
</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 })%>
item.Name %>
item.City %>
item.State %>
item.Code %>

hPot-Tech

66

MVC CRUD using .NET SDK

<%:
</td>
<td>
<%:
</td>
<td>
<%:
</td>
<td>
<%:
</td>
<td>
<%:
</td>
<td>
<%:
</td>
<td>
<%:
</td>
</tr>

item.Country %>
item.Phone %>
item.Website %>
String.Format("{0:g}", item.Updated) %>
item.Description %>
item.Type %>
item.Id %>

<% } %>
</table>
<p>
<%: Html.ActionLink("Create New", "Create") %>
</p>
</asp:Content>

hPot-Tech

67

MVC CRUD using .NET SDK

Web.config

<?xml version="1.0"?>
<!-For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=152368
-->
<configuration>
<configSections>
<section name="couchbase" type="Couchbase.Configuration.CouchbaseClientSection, Couchbase"/>
</configSections>
<couchbase>
<servers bucket="beer-sample">
<add uri="http://127.0.0.1:8091/pools"/>
</servers>
</couchbase>
<connectionStrings>
<add name="ApplicationServices"
connectionString="data source=.\SQLEXPRESS;Integrated
Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
</connectionStrings>

hPot-Tech

68

MVC CRUD using .NET SDK

<system.web>
<compilation debug="true" targetFramework="4.0">
<assemblies>
<add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
/>
</assemblies>
</compilation>
<authentication mode="Forms">
<forms loginUrl="~/Account/LogOn" timeout="2880" />
</authentication>
<membership>
<providers>
<clear/>
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider"
connectionStringName="ApplicationServices"
enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false"
requiresUniqueEmail="false"
maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6"
minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
applicationName="/" />
</providers>
</membership>
<profile>
<providers>
<clear/>
<add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider"
connectionStringName="ApplicationServices" applicationName="/" />
</providers>
</profile>
hPot-Tech

69

MVC CRUD using .NET SDK

<roleManager enabled="false">
<providers>
<clear/>
<add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider"
connectionStringName="ApplicationServices" applicationName="/" />
<add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider"
applicationName="/" />
</providers>
</roleManager>
<pages>
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
</namespaces>
</pages>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>

hPot-Tech

70

MVC CRUD using .NET SDK

</configuration>

hPot-Tech

S-ar putea să vă placă și