我很努力地理解用戶在頁面上時我在哪裏保留viewmodel。我有幾個用戶控件,使AJAX請求和應用binging在用戶控件中的控件。我正在使用映射插件來填充我的視圖模型。我在頁面上有4-5個用戶控件。我努力將視圖模型保存在內存中,以便它可以檢測到更改並將其發送回服務器。截至目前,我將它們保存在window.Model1屬性中,這不是一個好主意。knockout.js:如果AJAX調用在usercontrol中保存viewmodel的位置
有人可以告訴我什麼是保持viewmodels在內存中的最佳方式,以便我可以檢測到更改?還是說我做的是錯誤的,有更好的方法來處理這種情況。
這裏是所有的代碼。
用戶控件:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ucCustomer.ascx.cs" Inherits="WebApplication2.UserControl.ucCustomer" %>
<input data-bind="value: FirstName" /><br />
<span>FirstName: </span><span data-bind="text: FirstName"></span><br />
<input data-bind="value: FirstName" /><br />
<span>FirstName: </span><span data-bind="text: LastName"></span><br />
<script type="text/javascript">
$.ajax({
type: "POST",
url: "ws/GetData.asmx/GetCustomer",
cache: false,
contentType: "application/json; charset=utf-8",
data: "{}",
dataType: "json",
success: handleHtml,
error: ajaxFailed
});
function handleHtml(data, status) {
var myViewModel = ko.mapping.fromJS(data.d);
window.myViewModel = myViewModel;
ko.applyBindings(myViewModel);
}
function ajaxFailed(xmlRequest) {
alert(xmlRequest.status + ' \n\r ' +
xmlRequest.statusText + '\n\r' +
xmlRequest.responseText);
}
</script>
ASPX頁面:
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="WebApplication2._Default" %>
<%@ Register Src="UserControl/ucCustomer.ascx" TagName="ucCustomer" TagPrefix="uc1" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script src="Scripts/knockout.debug.js" type="text/javascript"></script>
<script src="Scripts/knockout.mapping-latest.js" type="text/javascript"></script>
<script type="text/javascript">
function SendDataBackToServer() {
var arrayList = new ArrayList();
arraylist[0] = window.myViewModel;
arraylist[1] = window.myViewModel1;
arraylist[2] = window.myViewModel2;
//Make an AJAX call here and send arrayList back to server
return false;
}
</script>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<div id="dvCust">
<uc1:ucCustomer ID="ucCustomer1" runat="server" />
</div>
<div>
<button title="Send Data Back" onclick="JavaScript: return SendDataBackToServer();">
Send Data Back To Server</button>
</div>
</asp:Content>
Web服務:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using WebApplication2.DataModel;
namespace WebApplication2.WS
{
/// <summary>
/// Summary description for GetData
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class GetData : System.Web.Services.WebService
{
[WebMethod]
public Customer GetCustomer()
{
return new Customer
{
FirstName = "FName",
LastName = "LName"
};
}
}
}
客戶型號:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApplication2.DataModel
{
public class Customer
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
}
看看這篇文章,它幫了我很多:) [使用與asp.net淘汰賽](http://www.codeproject.com/Articles/153735/Using-KnockoutJS-in-your -ASP-NET-應用程序) – Arnstein 2012-10-24 09:05:39