Use AngularJs language in ASP. net mvc (V): ng-selected,
This time I learned the ng-selected syntax. This is the default option for the DropDownList drop-down list.
This example starts with the following steps:
1. Create a model:
The property of the above #14 lines of code. The data type is bool. That is, whether the storage option is selected or not, true or false.
Public class Car {public int ID {get; set;} public string Name {get; set;} public bool Selected {get; set ;}}Source Code
2. Create an Entity: Prepare data:
Public class CarEntity {public IEnumerable <Car> Cars () {return new List <Car> () {new Car () {ID = 1, Name = "Maserati ", selected = false }}, {new Car () {ID = 2, Name = "Benz", Selected = false }}, {new Car () {ID = 3, name = "BMW", Selected = true },{ new Car () {ID = 4, Name = "Porsche", Selected = false }}};}}Source Code
3. Prepare the Controller for ASP. net mvc:
Public class CarController: Controller {// GET: Car public ActionResult Index () {return View ();} public JsonResult GetCars () {CarEntity ce = new CarEntity (); var model = ce. cars (); return Json (model, JsonRequestBehavior. allowGet );}}Source Code
4. Create an ng-app in this step. Refer to the sixth step in this series of articles. Use AngularJs language (1): Hello your name http://www.cnblogs.com/insus/p/8520555.htmlin ASP. net mvc.
5. Create ng-controller:
PilotApp. controller ('carctrl ', [' $ http', '$ scope', function ($ http, $ scope) {var obj ={}; $ http ({method: 'post', url: '/Car/GetCars', ype: 'json', headers: {'content-type': 'application/json; charset = UTF-8 '}, data: JSON. stringify (obj ),}). then (function (response) {$ scope. cars = response. data ;}) ;}]);Source Code
The last step is to implement the ASP. net mvc View:
<Div ng-app = "PilotApp" ng-controller = "CarCtrl"> <select> <option value = "0" label = "Please Select"> </option> <option ng-repeat = "Car in Cars" value = "{Car. id }}" ng-selected = "{Car. selected = true }}" >{{ Car. name }}</option> </select> </div>Source Code
Demo:
From the above Entity class, we can see that the BMW line is Selected.
Therefore, initialization of "BMW" is optional no matter how the page is refreshed.