Use Knockout practice 08 in ASP. net mvc, use foreach to bind a set, and use knockoutforeach

Source: Internet
Author: User

Use Knockout practice 08 in ASP. net mvc, use foreach to bind a set, and use knockoutforeach

This article uses foreach to bind a Product set.

 

Create a View Model using the constructor.

        var Product = function(data) {
            this.name = ko.observable(data.name);
            this.category = ko.observable(data.category);
        };

Because json data is returned from the server, when the server returns the Products set, you can construct a json format with the key as name and category.

 

Foreach needs to bind a set. We also need to create a View Model that has a set attribute.

        var RealVM = function(products) {
            var productsArr = [];
            for (var i = 0; i < products.length; i++) {
                var product = new Product(products[i]);
                productsArr.push(product);
            }
            this.products = ko.observableArray(productsArr);
        };   


Next, send an asynchronous request to the server, and the returned Prduct set serves as the real parameter of the RealVM constructor.

            $.getJSON('@Url.Action("GetProducts","Home")', function(data) {
                var vm = new RealVM(data);
                ko.applyBindings(vm);
            });    


The complete front-end code is as follows:

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<table>
    <thead>
        <tr>
<Th> name </th>
<Th> Category </th>
        </tr>
    </thead>
    <tbody data-bind="foreach:products">
        <tr>
            <td data-bind="text: name"></td>
            <td data-bind="text: category"></td>
        </tr>
    </tbody>
</table>
@section scripts
{
    <script src="~/Scripts/knockout-3.2.0.js"></script>
    <script src="~/Scripts/knockout.validation.js"></script>
    <script src="~/Scripts/zh-CN.js"></script>
    <script type="text/javascript">
// Use the constructor to define the View Model and use data as the parameter.
        var Product = function(data) {
            this.name = ko.observable(data.name);
            this.category = ko.observable(data.category);
        };
        var RealVM = function(products) {
            var productsArr = [];
            for (var i = 0; i < products.length; i++) {
                var product = new Product(products[i]);
                productsArr.push(product);
            }
            this.products = ko.observableArray(productsArr);
        };
        
// Send an asynchronous request to the server after the page is loaded
        $(function () {
            $.getJSON('@Url.Action("GetProducts","Home")', function(data) {
                var vm = new RealVM(data);
                ko.applyBindings(vm);
            });
        });
    </script>
}

 

HomeController code:

        static readonly IProductRepository repository = new ProductRepository();
        public JsonResult GetProducts()
        {
            var allProducts = repository.GetAll();
            var result = from p in allProducts
                select new {name = p.Name, category = p.Category};
            return Json(result, JsonRequestBehavior.AllowGet);
        }

 

Conclusion: Using foreach to bind a View Model that provides the set attributes, you can use ko. observableArray () to make the set attributes have Observable.


The following code is part of the code aspnet mvc 3. When running, the system prompts that foreach has not set the object reference to the instance of the object?

Hwnd = Plugin. Window. GetKeyFocusWnd ()
Delay2000

T1 = now: t2 = now: sj1 = 0.5: sj2 = 60
Rem abcc
Delay 20
If DateDiff ("s", t1, now)> = sj1

Delay 10

Call Plugin. Bkgnd. KeyPress (Hwnd, 83)
Delay 10
T1 = now
End If
If DateDiff ("s", t2, now)> = sj2
Delay 10
Call Plugin. Bkgnd. KeyPress (Hwnd, 70)
T2 = now
End If
Goto abcc

In aspnet mvc, view generally uses foreach to retrieve the list. What should I do if I use? Best example

For (int I = 0; I <model. Count; I ++ ){
// Your content
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.