我很难理解当用户在页面上时我在哪里保留视图模型。我有几个用户控件发出 AJAX 请求并将绑定应用于用户控件中的控件。我正在使用映射插件来填充我的视图模型。我在页面上有 4-5 个用户控件。我正在努力将视图模型保存在内存中,以便它可以检测到更改并将其发送回服务器。截至目前,我将它们保存在 window.Model1 属性中,这不是一个好主意。
有人可以告诉我将视图模型保存在内存中以便我可以检测到更改的最佳方法是什么?还是我做错了,有更好的方法来处理这种情况。
这是所有这些的代码。
用户控制:
<%@ 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>
页面:
<%@ 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>
网络服务:
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; }
}
}