0

I am asp.net MVC developer.

In my model as follows,

public class GiftModel
{
    public string Title { get; set; }
    public double Price { get; set; }
}

controller=>

public ActionResult Test()
{
    ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";

    GiftModel[] initialState = new[] {
        new GiftModel { Title = "Tall Hat", Price = 49.95 },
        new GiftModel { Title = "Long Cloak", Price = 78.25 }
    };
    return View(initialState);
}

but when executing I stuck here in script ,

initialValues= ""[{\"Title\":\"Tall Hat...ak\",\"Price\":78.25}]""

var result = ko.observable(initialValues)

this output no result

1
  • 1
    Your qeustion is very unclear! What are you trying to do? Please post your actual view code html and js and also try to describe what is not working! Commented Aug 30, 2013 at 8:38

1 Answer 1

2

From what I understand, you need to create an observableArray, but the problem is your initialValues is a string. You need to convert your string into object using JSON.parse:

initialValues= JSON.parse("[{\"Title\":\"Tall Hat...ak\",\"Price\":78.25}]");
var result = ko.observableArray(initialValues)
Sign up to request clarification or add additional context in comments.

3 Comments

@Aruni Godage: please give more information in your question.
JSON.parse() => what is does khanh ? I new to this.that return no result.
@Aruni Godage: it converts your json string into javascript object. More information developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.