Skip to content

Instantly share code, notes, and snippets.

@BretStateham
Created October 1, 2013 16:46
Show Gist options
  • Save BretStateham/6781508 to your computer and use it in GitHub Desktop.
Save BretStateham/6781508 to your computer and use it in GitHub Desktop.
"Windows Azure Web Sites and Virtual Machines using ASP.NET and SQL Server" Hands-On Lab Completed Code.
namespace ContactManager.Web.Models
{
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
public class Contact
{
public int Id { get; set; }
[Required]
[DisplayName("First Name")]
public string FirstName { get; set; }
[DisplayName("Last Name")]
[Required]
public string LastName { get; set; }
[DataType(DataType.EmailAddress)]
[StringLength(50)]
public string Email { get; set; }
[StringLength(50, MinimumLength = 3, ErrorMessage = "Must have a minimum length of 3.")]
public string Company { get; set; }
[DisplayName("Business Phone")]
[DataType(DataType.PhoneNumber)]
public string BusinessPhone { get; set; }
[DisplayName("Mobile Phone")]
[DataType(DataType.PhoneNumber)]
public string MobilePhone { get; set; }
[StringLength(50, MinimumLength = 3, ErrorMessage = "Must have a minimum length of 3.")]
[DataType(DataType.Text)]
public string Address { get; set; }
[StringLength(50, MinimumLength = 3, ErrorMessage = "Must have a minimum length of 3.")]
[DataType(DataType.Text)]
public string City { get; set; }
[StringLength(10)]
[DataType(DataType.Text)]
public string Zip { get; set; }
}
}
namespace ContactManager.Web.Models
{
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.SqlClient;
public class ContactContext : DbContext
{
public ContactContext()
: base("ContactManagerDb")
{
}
public DbSet<Contact> Contacts { get; set; }
public IEnumerable<Contact> SearchContacts(string searchQuery)
{
return this.Database.SqlQuery<Contact>("EXECUTE [dbo].[SearchContacts] @searchQuery", new SqlParameter("searchQuery", searchQuery ?? string.Empty));
}
}
}
@model ContactManager.Web.Models.Contact
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>Contact</legend>
<div class="editor-label">
@Html.LabelFor(model => model.FirstName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.FirstName)
@Html.ValidationMessageFor(model => model.FirstName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.LastName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.LastName)
@Html.ValidationMessageFor(model => model.LastName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Email)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Email)
@Html.ValidationMessageFor(model => model.Email)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Company)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Company)
@Html.ValidationMessageFor(model => model.Company)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.BusinessPhone)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.BusinessPhone)
@Html.ValidationMessageFor(model => model.BusinessPhone)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.MobilePhone)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.MobilePhone)
@Html.ValidationMessageFor(model => model.MobilePhone)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Address)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Address)
@Html.ValidationMessageFor(model => model.Address)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.City)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.City)
@Html.ValidationMessageFor(model => model.City)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Zip)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Zip)
@Html.ValidationMessageFor(model => model.Zip)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
@model IEnumerable<ContactManager.Web.Models.Contact>
@{
ViewBag.Title = "List";
}
<section class="featured">
<div class="content-wrapper">
<hgroup class="title">
<h1>Contact Manager Sample</h1>
<h2>for Windows Azure Web Sites</h2>
</hgroup>
<p>
Create and search for contacts using Full-Text Search in SQL Server 2012 - try queries like &quot;Joh*&quot; (with the quotation marks) and john OR smith OR contoso
</p>
</div>
</section>
@using (Html.BeginForm())
{
<fieldset class="search">
<label>Search Contacts by First Name/Last Name/Company: </label>
@Html.TextBox("searchQuery")
<input type="submit" value="Search" />
</fieldset>
}
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.FirstName)
</th>
<th>
@Html.DisplayNameFor(model => model.LastName)
</th>
<th>
@Html.DisplayNameFor(model => model.Email)
</th>
<th>
@Html.DisplayNameFor(model => model.Company)
</th>
<th>
@Html.DisplayNameFor(model => model.BusinessPhone)
</th>
<th>
@Html.DisplayNameFor(model => model.MobilePhone)
</th>
<th>
@Html.DisplayNameFor(model => model.Address)
</th>
<th>
@Html.DisplayNameFor(model => model.City)
</th>
<th>
@Html.DisplayNameFor(model => model.Zip)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.FirstName)
</td>
<td>
@Html.DisplayFor(modelItem => item.LastName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Email)
</td>
<td>
@Html.DisplayFor(modelItem => item.Company)
</td>
<td>
@Html.DisplayFor(modelItem => item.BusinessPhone)
</td>
<td>
@Html.DisplayFor(modelItem => item.MobilePhone)
</td>
<td>
@Html.DisplayFor(modelItem => item.Address)
</td>
<td>
@Html.DisplayFor(modelItem => item.City)
</td>
<td>
@Html.DisplayFor(modelItem => item.Zip)
</td>
</tr>
}
@if (Model.Count() == 0)
{
<tr>
<td colspan="10">
No contacts found
</td>
</tr>
}
</table>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<add name="ContactManagerDb" connectionString="Data Source=sqlvm2012.cloudapp.net,57501;Initial Catalog=ContactManagerDb;User Id=TestUser;Password=P@ssw1rd;" providerName="System.Data.SqlClient" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment