在视图状态上保留数据并中继到 Knockout.JS

Persisting data on view state and relaying to Knockout.JS

我目前正在开发一个使用 Knockout、Bootstrap 和 JQuery 的 asp.net 网络表单。我在 "Wizard steps."

中遇到数据持久化问题

我想做的是获取客户端的邮件状态并将其持久化到其他页面,然后使用 Knockout 使字段可见并创建验证。

我读到您可以使用隐藏状态来完成此操作,但我在将值传递给 Knockout 并最终显示其他字段时遇到了问题。

这是一页一页执行所有步骤的 c#。

    protected void StepChanged(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            if (QuoteWizard.ActiveStepIndex == 0)
                txtFirstName.Focus();

            if (QuoteWizard.ActiveStepIndex == 1)
            {
                Session["State"] = ddlState.Value;
                rblMailAddr.Focus();
            }

            if (QuoteWizard.ActiveStepIndex == 3)
            {
                txtDriverFName1.Value = txtFirstName.Value;
                txtDriverMI1.Value = txtMI.Value;
                txtDriverLName1.Value = txtLastName.Value;

                String DOB;
                DOB = Convert.ToDateTime(txtDOB.Value).ToString();
                txtDriverDOB1.Value = DOB;

                txtDriverFName1.Focus();
            }

我觉得 txtDriverFName1.Value = txtFirstNAme.Value 很奇怪;正确通过,但我无法从下拉列表中获取状态或出生日期从一步传递到另一步。

   <select id="ddlState" runat="server" class="form-control" data-bind="value: MailState, updateValue: 'afterkeydown'">

后面是状态列表,然后我尝试将它传递给 knockout 以使字段可见:

    self.MailState = ko.observable("", { persist: "MailState" });

但是,一旦我到达下一步,ViewState 中的值就会被删除并且

    <div class="btn-group" data-toggle="buttons" style="padding-left: 10px" data-bind="visible: MailState() == 'CA'">

即使在之前的视图状态中选择了 CA,也不再可见。

那么我如何通过 QuoteWizard.ActiveStepIndex 中的 2 个或更多步骤保留我的下拉列表的值并将其分配给 "MailState()" 随后激活 Knockout?

在 bindinghandler 句柄中数据绑定以执行表达式,然后 visible bindinghandler 会将其解包为可观察值。但是在你的代码中你使用

data-bind="visible: MailState == 'CA'" //MailState is observalbe then like function() == 'CA' - that not right

应更改为:

data-bind="visible: MailState() == 'CA'"