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

4

1 回答 1

2

据我了解,您需要创建一个 observableArray,但问题是您initialValues是一个字符串。您需要使用以下方法将字符串转换为对象JSON.parse

initialValues= JSON.parse("[{\"Title\":\"Tall Hat...ak\",\"Price\":78.25}]");
var result = ko.observableArray(initialValues)
于 2013-08-30T08:49:22.787 回答