Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7501159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:26:43+00:00 2026-05-29T20:26:43+00:00

Perhaps there is an easy solution for my problem but I simply cannot seem

  • 0

Perhaps there is an easy solution for my problem but I simply cannot seem to find it. I have read lots of tutorials about Knockout so I get the basics but I ask this question because my entity-structure is a bit more complicated than a person with a name and a list of friends which may or may not be on Twitter (Video on Channel9: Helping you build dynamic JavaScript UIs with MVVM and ASP.NET). Here’s my situation:

I have a class PersonnelClass with this basic structure:

[Serializable]
//The interface is for the implementation of 'Name' and 'Description'
public class PersonnelClass : IPersonnelClassOrPerson 
{
    public PersonnelClass() : this(Guid.NewGuid(), "", "") { }

    public PersonnelClass(Guid id, String name, String description = null)
    {
        if (id == Guid.Empty) { throw new ArgumentNullException("id"); }
        Id = id;
        Name = name;
        Description = description;
        Properties = new PropertyCollection();
    }

    public Guid Id { get; private set; }
    public String Name { get; set; }
    public String Description { get; set; }
    public PropertyCollection Properties { get; private set; }
}

The PropertyCollection class and associated AbstractProperty class look like this:

[Serializable]
public class PropertyCollection: List<AbstractProperty> { }

[Serializable]
public abstract class AbstractProperty: IEntity, IProperty
{
    public AbstractProperty(String name, String description = null) : this(Guid.NewGuid(), name, description) { }

    public AbstractProperty(Guid id, String name, String description = null)
    {
        if (id == Guid.Empty) { throw new ArgumentNullException("id"); }
        if (String.IsNullOrEmpty(name)) { throw new ArgumentNullException("name"); }
        Id = id;
        Name = name;
        Description = description;
    }

    public Guid Id { get; private set; }
    public String Name { get; private set; }
    public String Description { get; private set; }
}

In my Controller, I create an instance of a PersonnelClassViewModel that has this structure:

public class PersonnelClassViewModel
{
    public PersonnelClass PersonnelClass { get; set; }
    public List<AbstractProperty> Properties { get; set; }
}

I fill this viewmodel with a new PersonnelClass and two test-properties to pass to my View like this:

var properties = new List<AbstractProperty>
{
    new TextProperty("prop1", "descr1"),
    new TextProperty("prop2", "descr2")
    //TextProperty is derived from AbstractProperty
};
var vm = new PersonnelClassViewModel { Properties = properties };
return View(vm);

I get everything in my View as I wanted. From the View I want to create a new PersonnelClass with a set of selected properties. I have the fields for Name and Description and to add the properties I have a ListBox with the properties that already exist (for demo-purpose they came from the controller now). Through a bit of Knockout JavaScript code I can select items from this list and populate an HTML select-control () with the selected properties to add to the PersonnelClass. This all works fine, until I want to build up an object to pass back to the Controller and create the PersonnelClass.

My question is: what Knockout JS code is needed to build up this object and pass it to the Controller by submitting the form and in my Controller how should I receive this object, meaning: what type of object should this be (PersonnelClass, PersonnelClassViewModel, …) ?

If any more info/code is needed, please do ask. Thanks in advance!

Update after answer of ‘B Z’:

I followed a few more of Steven Sanderson’s tutorials about this to be sure I understand this, especially the one you provided in your answer. Now I have following code in my View to start with:

var initialData = @Html.Raw(new JavaScriptSerializer().Serialize(Model));

var viewModel = {
    personnelClassViewModel : ko.mapping.fromJS(initialData),
    properties : personnelClassViewModel.Properties,
    selectedProperties : ko.observableArray([]),
    addedProperties : ko.observableArray([])
};

ko.applyBindings(viewModel);

The variable ‘initialData’ contains the values I expect it to have but then I get the following error:

Microsoft JScript runtime error: ‘personnelClassViewModel’ is undefined

I have no clue anymore. Can anyone help me fix this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T20:26:46+00:00Added an answer on May 29, 2026 at 8:26 pm

    Steven Sanderson has an example of how to to work with variable length lists and knockoutjs

    http://blog.stevensanderson.com/2010/07/12/editing-a-variable-length-list-knockout-style/

    Having said that, I think your problem isn’t so much on the knockout side and more on the how to databind the data correctly on the server side. In the link above, Steven uses a FromJson attribute to model bind which you may find useful…

    HTH

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone have a good VBA way (or perhaps there is an MS Word
I haven't found an answer to this particular question; perhaps there isn't one. But
Is there an equivalent to 'intellisense' for Python? Perhaps i shouldn't admit it but
Is there an easy way to get the relative complement of two sets? Perhaps
I'm perhaps overlooking something, but I'm attempting to wrestle protocol buffers into an easy
I'm stuck on a small but tricky problem since yesterday. What I have is
This seems like it would be an easy solution, but I'm wasting too much
Is there a way to get an object from a specific namespace? Perhaps with
Are there any editors that can edit multi-gigabyte text files, perhaps by only loading
is there anyway to save the following flash video locally, perhaps by streaming it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.