Knockout can be used to obtain array element indexes. It is implemented in MVC. knockoutmvc

Source: Internet
Author: User

Knockout can be used to obtain array element indexes. It is implemented in MVC. knockoutmvc

When traversing arrays and sets, you usually need to obtain the element index. In this article, you can use two methods to obtain the index using Knockout.

 

Suppose there is a model like this:

namespace UseIndex.Models{    public class Student    {        public int Id { get; set; }        public string Name { get; set; }    }}

 

In HomeController, simulate a collection of Student, project the set of Name attributes, and return it to the foreground view in Json format.

Using System. collections. generic; using System. linq; using System. web. mvc; using UseIndex. models; namespace UseIndex. controllers {public class HomeController: Controller {public ActionResult Index () {return View ();} public JsonResult GetStudentNames () {var students = new List <Student> () {new Student () {Id = 1, Name = "Xiao Ming"}, new Student () {Id = 2, Name = "Xiao Tian "}}; var names = from student in students select new {student. name}; return Json (names, JsonRequestBehavior. allowGet );}}}

 

In Home/Index. cshtml:

@ {Layout = null ;}<! DOCTYPE html> 

Above, $ data refers to the current set element. $ Root indicates the ViewModel in the root context.

 

Run:

 

Summary

There are two ways to obtain the index of a set or array element:
1. data-bind = "text: $ index" or data-bind = "text: $ index () + 1"
2. In View Model, obtain the context first, and then context. $ index ()


Js to obtain the string index of array elements ,~

Var array = new Array ();
Array [0] = "1 ";
Array [1] = "2 ";
Array [2] = "3 ";
Var index = array. length;
For (var I = 0; I <index; I ++ ){
Document. writeln (the index of array [I] + "is:" + I );
}
 
Write a java program, copy the elements in index location 03 of array arr1 to array arr2, and output the arr1 and arr2 elements,

Is there a red line under the word copyOf, and the system prompts that this method is not defined?
-|
I heard people say: Eclipse does not come with this method, which is available in JDK 1.6.0.
So, check whether JDK6.0 is installed and you use it.

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.