// ContactController class -----------------------------------------------
public ActionResult GetContactsJSON(string id)
{
if (string.IsNullOrEmpty(id))
{
return Json(_ctx.Contacts.OrderBy(cnt => cnt.FirstName).Take(100).ToList(), JsonRequestBehavior.AllowGet);
}
else
{
return Json(_ctx.Contacts.Where(cnt => cnt.FirstName.StartsWith(id)).OrderBy(cnt => cnt.FirstName).Take(100).ToList(), JsonRequestBehavior.AllowGet);
}
}
public ActionResult GetContactJSON(int id)
{
return Json(_ctx.Contacts.SingleOrDefault(cnt => cnt.ContactID == id), JsonRequestBehavior.AllowGet);
}
// Master Page -----------------------------------------------
// add the jquery script reference
//+ add ContentPlcaHolder to allow content page to write the script block
<head runat="server">
<title><asp:ContentPlaceHolder ID="TitleContent" runat="server" />title>
<link href="../../Content/Site.css" rel="stylesheet" type="text/css" />
<script src="../../Scripts/jquery-1.4.1.js" type="text/javascript">script>
<asp:ContentPlaceHolder ID="HeaderPlaceHolder" runat="server"/>
head>
// Detail Page -----------------------------------------------
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Details
asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="HeaderPlaceHolder" runat="server">
<script type="text/javascript">
$(document).ready(function () {
//alert("Test");
// $.getJSON("/Contact/GetContactsJSON", null, function (data) {
$.getJSON("/Contact/GetContactsJSON", null, function (data) {
for (i = 0; i < data.length; i++) {
$('#db_contacts').append($("").attr("value", data[i].ContactID).text(data[i].ShortFormat));
} //for
$('#db_contacts').change(function (data) {
populate($("#db_contacts option:selected").attr('value'));
}); //change
}); //getJSON
$("#txt_search").keyup(populateList);
$("#btn_search").click(populateList);
}); //ready
function populateList() {
$.getJSON("/Contact/GetContactsJSON/" + $("#txt_search").val(), null, function (data) {
$('#db_contacts').find('option').remove();
for (i = 0; i < data.length; i++) {
$('#db_contacts').append($("").attr("value", data[i].ContactID).text(data[i].ShortFormat));
} //for
}); //getJSON
} //populateList
function populate(key) {
$.getJSON("/Contact/GetContactJSON/" + key.toString(), null, function (data) {
$('#ContactID').text(data.ContactID);
$('#NameStyle').text(data.NameStyle);
$('#Title').text(data.Title);
$('#FirstName').text(data.FirstName);
$('#MiddleName').text(data.MiddleName);
$('#LastName').text(data.LastName);
}); // getJSON
} //opulate
script>
<style type="text/css">
#Select1
{
width: 275px;
}
style>
asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<p>
search <span width="30px"/> <input id="txt_search" type="text" /> <span width="30px"/>
<input id="btn_search" type="button" value="search" />
p>
<select id="db_contacts" name="db_contacts">
select>
<h2>
Detailsh2>
<fieldset>
<legend>Fieldslegend>
<div class="display-label">
ContactIDdiv>
<div class="display-field" id="ContactID">
<%: Model.Current.ContactID %>div>
<div class="display-label">
NameStylediv>
<div class="display-field" id="NameStyle">
<%: Model.Current.NameStyle %>div>
<div class="display-label">
Titlediv>
<div class="display-field" id="Title">
<%: Model.Current.Title %>div>
<div class="display-label">
FirstNamediv>
<div class="display-field" id="FirstName">
<%: Model.Current.FirstName %>div>
<div class="display-label">
MiddleNamediv>
<div class="display-field" id="MiddleName">
<%: Model.Current.MiddleName %>div>
<div class="display-label">
LastNamediv>
<div class="display-field" id="LastName">
<%: Model.Current.LastName %>div>
<div class="display-label">
Suffixdiv>
<div class="display-field">
<%: Model.Current.Suffix %>div>
<div class="display-label">
EmailAddressdiv>
<div class="display-field">
<%: Model.Current.EmailAddress %>div>
<div class="display-label">
EmailPromotiondiv>
<div class="display-field">
<%: Model.Current.EmailPromotion %>div>
<div class="display-label">
Phonediv>
<div class="display-field">
<%: Model.Current.Phone %>div>
<div class="display-label">
PasswordHashdiv>
<div class="display-field">
<%: Model.Current.PasswordHash %>div>
<div class="display-label">
PasswordSaltdiv>
<div class="display-field">
<%: Model.Current.PasswordSalt %>div>
<div class="display-label">
AdditionalContactInfodiv>
<div class="display-field">
<%: Model.Current.AdditionalContactInfo %>div>
<div class="display-label">
rowguiddiv>
<div class="display-field">
<%: Model.Current.rowguid %>div>
<div class="display-label">
ModifiedDatediv>
<div class="display-field">
<%: String.Format("{0:g}", Model.Current.ModifiedDate) %>div>
<div class="display-label">
ShortFormatdiv>
<div class="display-field">
<%: Model.Current.ShortFormat %>div>
fieldset>
<p>
<%: Html.ActionLink("Edit", "Edit", new { id=Model.Current.ContactID }) %>
|
<%: Html.ActionLink("Back to List", "Index") %>
p>
asp:Content>
No comments:
Post a Comment