Solved

Getting KO'd

Posted on 2015-02-01
3
131 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
ID: 40583515
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
ID: 40583688
That was it!
Thank you very much!
wdg
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
This article will show, step by step, how to integrate R code into a R Sweave document
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

910 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

25 Experts available now in Live!

Get 1:1 Help Now