Solved

Getting KO'd

Posted on 2015-02-01
3
128 Views
Last Modified: 2015-02-02
I recently completed a tutorial on using Knockout, Entity Framework and Ajax.
The tutorial works great. I followed everything step by step and it was great.

Now I am trying to apply the steps to my own solution. It is a "Code First, Existing Database" project and it will really only be used as an input application. The applicants won't need to edit or delete or view any data after submit.

I am getting a confusing error on the save function: 0x800a139e - JavaScript runtime error: Pass a function that returns the value of the ko.computed.


I believe I have done the knockout mapping, the controller save, and the Create view script references exactly as the course demonstrates.
The relevant code:
FamilyViewModel.js
FamilyViewModel = function (data) {
    var self = this;
    ko.mapping.fromJS(data, {}, self);
}

self.save = function(){
    $.ajax({
    url: "/Family/Save/",
    type: "POST",
    data: ko.toJSON(self),
    contentType: "application/json",
    success: function(data) {
        if(data.FamilyViewModel != null)
            ko.mapping.fromJS(data.FamilyViewModel, {}, self)
    }
});
}

Open in new window


The Save method
        public JsonResult Save(FamilyViewModel familyViewModel)
        {
            Families families = new Families();
            families.FamilyName = familyViewModel.FamilyName;
            families.PhoneNumber = familyViewModel.PhoneNumber;
            families.Email = familyViewModel.Email;
            families.Address1 = familyViewModel.Address1;
            families.Address2 = familyViewModel.Address2;
            families.City = familyViewModel.City;
            families.State = familyViewModel.State;
            families.ZipCode = familyViewModel.ZipCode;
            families.MyPreferedSite = familyViewModel.MyPreferedSite;
            families.FamilyIncome = familyViewModel.FamilyIncome;

            _familyContext.Families.Add(families);
            _familyContext.SaveChanges();

            familyViewModel.MessageToClient = string.Format("Thank you {0}. Your family profile has been created.", families.FamilyName);

            return Json(new {familyViewModel});

        }

Open in new window


The Create.cshtml page
@model FamilyProfile.Web.ViewModels.FamilyViewModel
@using System.Web.Script.Serialization

@{
    ViewBag.Title = "Create Family Profile";
}

@{ string data = new JavaScriptSerializer().Serialize(Model);}

@section scripts
{
    <script src="~/Scripts/knockout-3.2.0.js"></script>
    <script src="~/Scripts/knockout.mapping-latest.js"></script>
    <script src="~/Scripts/familyViewModel.js"></script>
    <script type="text/javascript">
        var familyViewModel = new FamilyViewModel(@Html.Raw(data));
        ko.applyBindings(familyViewModel);
    </script>
}

<h2>@ViewBag.Title</h2>
<h4 data-bind="text: MessageToClient">@Html.DisplayFor(model => model.MessageToClient)</h4>

<div>
    <div>
        <label>Family Name</label>
        <input data-bind="value: FamilyName"/>
    </div>
    <div>
        <label>Family Phone</label>
        <input data-bind="value: PhoneNumber"/>
    </div>
    <div>
        <label>Family Email</label>
        <input data-bind="value: Email"/>
    </div>
    <div>
        <label>Street Address</label>
        <input data-bind="value: Address1"/>
    </div>
    <div>
        <label>Apt or Unit</label>
        <input data-bind="value: Address2"/>
    </div>
    <div>
        <label>City</label>
        <input data-bind="value: City"/>
    </div>
    <div>
        <label>State</label>
        <input data-bind="value: State"/>
    </div>
    <div>
        <label>Zip Code</label>
        <input  data-bind="value: ZipCode"/>
    </div>
    <div>
        <label>Income Range</label>
        <input data-bind="value: FamilyIncome"/>
    </div>
</div>
<p><button data-bind="click: save">Save</button></p>

Open in new window

0
Comment
Question by:wdarnellg
3 Comments
 
LVL 35

Accepted Solution

by:
Terry Woods earned 500 total points
Comment Utility
I've done a bit of Knockout, but not very recently. I think the self.save function needs to go within the FamilyViewModel template.

That's how it's done for the save function against the Cart in this example: http://knockoutjs.com/examples/cartEditor.html
0
 

Author Closing Comment

by:wdarnellg
Comment Utility
That was it!
Thank you very much!
wdg
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Developer tools in browsers have been around for a while, yet they are still heavily underused by developers. Developers still fix html or CSS then refresh page to see effect, or they put alert or debugger in JavaScript and then try again and again …
It’s a strangely common occurrence that when you send someone their login details for a system, they can’t get in. This article will help you understand why it happens, and what you can do about it.
This video teaches users how to migrate an existing Wordpress website to a new domain.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now