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 7718253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:07:18+00:00 2026-06-01T03:07:18+00:00

I am new to javascript and MVC 3. I am developing a sample application

  • 0

I am new to javascript and MVC 3. I am developing a sample application to get familiar with KnockoutJs.
I am passing a c# object with some properties to a controller. Than this object is passed to the View serialized as JSon. Then I am using the data with Knockout in my view and want to return this data back to the server. But the binding with the the server data fails for on of my properties.
Here is my code:

Model:

public class FranchiseInfo
    {
        public string FullName { get; set; }
        public string ShortName { get; set; }
        public List<string> ServerIps = new List<string>();
    }

Controller with sample data returning JSon to the View:

 public JsonResult Data()
        {
            FranchiseInfo franchiseInfo = new FranchiseInfo();
            franchiseInfo.FullName = "PokerWorld";
            franchiseInfo.ShortName = "PW";
            franchiseInfo.ServerIps.Add("192.111.1.3");
            franchiseInfo.ServerIps.Add("192.112.1.4");

            return Json(franchiseInfo, JsonRequestBehavior.AllowGet);
        }

Javascript file using knockout:

  $(function () {

function viewModel() {
    var self = this;
    self.FullName = ko.observable();
    self.ShortName = ko.observable();
    self.optionValues = ko.observableArray([]);
    self.ServerIps = ko.observableArray([]);

    $.getJSON("Home/Data", function (data) {
        self.FullName(data.FullName);
        self.ShortName(data.ShortName);
        self.optionValues([data.FullName, data.ShortName]);
        for (var i = 0; i < data.ServerIps.length; i++) {
            self.ServerIps.push({ name: ko.observable(data.ServerIps[i]) });
        }
    });

    self.addIp = function () {
        self.ServerIps.push({ name: ko.observable("0.0.0") });
    }

    self.showIps = function () {
        alert(self.ServerIps[name]);
    }

    self.save = function () {
        $.ajax({
            url: "Home/Save",
            type: "post",
            data: ko.toJSON({ FullName: self.FullName, ShortName: self.ShortName, ServerIps: self.ServerIp }),
            contentType: "application/json",
            success: function (result) { alert("result") }
        });
    }
};
ko.applyBindings(new viewModel);

View:

    Full Name:
<span data-bind="text: FullName"></span>
<input data-bind="value: FullName" />
</div>

<div>
Short Name:
<span data-bind="text: ShortName"></span>
</div>

<select data-bind="options: optionValues"></select>

<div data-bind="foreach: ServerIps">
    Name:
    <input data-bind="value: name" />
    <span data-bind="text: name" />
</div>

<div data-bind="text: ko.toJSON(ServerIps)"></div>


<button data-bind="click: addIp">Add IP</button>

<button data-bind="click: save">Save</button>

When Save button is clicked the data is sent to the server in Json format:

Here is the controller:

public JsonResult Save(FranchiseInfo franchiseInfo)
        {
            //some data here
            //return Json result
        }

Full name and Short name properties bind correctly with the c# model when I am sending them in Json format back to the server but the ServerIps property which is an array cannot bind. I think because it is in the format { name: ip} and the model property ServerIps is of type List. How can I fix this ? Any help with working example will be appreciated. Thanks.

  • 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-06-01T03:07:20+00:00Added an answer on June 1, 2026 at 3:07 am

    I had the same problem in Java Spring.
    We solved it by serializing the ViewModel as a request string.

    We wrote the function ourselves (although you might want to check if the ‘value’ is an array and go a bit recursive):

    function serializeViewModelToPost(dataString) {
    var data = ko.toJS(dataString);
    var returnValue = '';
    $.each(data, function (key, value) {
    
            returnValue += key + '=' + value + '&';
    
    });
    
    return returnValue;
    }
    

    Another option is to parse it serverside:
    link

    UPDATE:

    self.save = function () {
        $.ajax({
            url: "Home/Save",
            type: "post",
            data: serializeViewModelToPost(this)),
            success: function (result) { alert("result") }
            });
    

    You still need to edit the serialize function to check for arrays.

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

Sidebar

Related Questions

I'm really new to ASP.NET MVC, and I'm trying to integrate some Javascript into
I'm working on my first ASP.NET MVC 3 application with utilizes some JavaScript and
I am new to MVC. I was using javascript alert in previous application from
Im new to MVC. I have a controller for some charts (ChartsController), where I
I am right now developing a dead simple application using backbonejs mvc javascript library.Just
I am new to JQuery. I am developing a ASP.Net MVC application. In one
I tried to integrate this javascript mvc sample http://www.alexatnet.com/content/model-view-controller-mvc-javascript into an asp.net page, I'm
I'm very new to both the Mvc framework as well as JavaScript and JQuery.
I created a new ASP .net mvc 2 web application with that default template
So, I'm working in a hybrid web app/mvc application. Trying to do all new

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.