为了账号安全,请及时绑定邮箱和手机立即绑定

ASP.NET MVC下使用ng-selected

标签:
产品

 这次学习ng-selected语法,这个是为DropDownList下拉列表显示默认选项。

 演示从下面步骤开始

1,新建一个model:


上面#14行代码的property,数据类型为bool。即是存储选项是否为选中与否,true或false。

 public class Car    {        public int ID { get; set; }        public string Name { get; set; }        public bool Selected { get; set; }    }

Source Code

 

2,创建一个Entity:准备数据:

 

 public class CarEntity    {        public IEnumerable<Car> Cars()        {            return new List<Car>()            {                {new Car() { ID = 1, Name = "玛莎拉蒂",Selected=false }},                {new Car() { ID = 2, Name = "奔驰" ,Selected=false }},                {new Car() { ID = 3, Name = "宝马" ,Selected=true }},                {new Car() { ID = 4, Name = "保时捷",Selected=false  }}            };        }    }

Source Code

 

 3,准备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,这一步骤,创建ng-app,参考这一系列文章的第一篇的第六步《ASP.NET MVC下使用AngularJs语言(一):Hello your name》http://www.cnblogs.com/insus/p/8520555.html。
5,创建ng-controller:


pilotApp.controller('CarCtrl', ['$http', '$scope',    function ($http, $scope) {        var obj = {};        $http({            method: 'POST',            url: '/Car/GetCars',            dataType: 'json',            headers: {                'Content-Type': 'application/json; charset=utf-8'            },            data: JSON.stringify(obj),        }).then(function (response) {            $scope.Cars = response.data;        });    }]);

Source Code

 

 最后一步,是实现ASP.NET MVC的视图:

 

<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

 

 演示:

从上面的Entity类中,可见 “宝马”这行是Selected的。
因此,不管页面怎样刷新后,初始化"宝马"为选上的。

 

                                                                                                                               
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
移动开发工程师
手记
粉丝
64
获赞与收藏
367

关注作者,订阅最新文章

阅读免费教程

  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消