B/S开发框架_怎么为dropdownlist下拉框_绑定后台数据库值

B/S开发框架中dropdownlist控件是经常用到的,有的会绑定数据库里的值,有的会绑定枚举值,有的是界面直接固定的,在云微web开发框架中我们究竟怎么为dropdownlist下拉框绑定数据库的值呢,我们以产品库存前端绑定产品信息为例获取产品信息为例给大家讲解,请跟我们一步一步来做。

数据库表及值

后台获取值并赋值

传值并前端绑定

总结


数据库表及值

我们新建一个产品库存表tb_ProductStore,ProductStore前端模型如下:


[Table("tb_ProductStore")]
    public partial class ProductStore
    {
        public int Id { get; set; }

        [Required] 
        [Display(Name = "产品")] 
        public int ProductId { get; set; }
        public Product Product { get; set; }
        
        [Required] 
        [Display(Name = "仓库")] 
        public int StoreId { get; set; }
        public Store Store { get; set; }
 
        [Required] 
        [Display(Name = "仓位")] 
        public int SiteId { get; set; }
        public Site Site { get; set; }

        [Required] 
        [Display(Name = "库存")] 
        public int StoreNum { get; set; }

        [Required] 
        [Display(Name = "描述")] 
        public string Remark { get; set; }

        [Required] 
        [Display(Name = "扩展")] 
        public string Extend1 { get; set; }

        public DateTime? CreateAt { get; set; }
        public string CreateBy { get; set; }
        public DateTime? ModifyAt { get; set; }
        public string ModifyBy { get; set; }
        /// <summary>
        /// a/m/d 新增/修改/删除
        /// </summary>
        [NotMapped]
        public string EditStatu { get; set; }
    }

数据库表结构如图:

表结构

后台获取值及赋值

SaleShippedController.cs
this._addinventoryService = addinventoryService;

IList<SelectListItem> productlist = new List<SelectListItem>();
_productService.GetAllProducts().ToList().ForEach(t => { productlist.Add(new SelectListItem() { Value = t.Id.ToString(), Text = t.Name }); });
ViewBag.Product = productlist;

List<SelectListItem> storeList = new List<SelectListItem>();
_storeService.GetAllStores().ToList().ForEach(t => { storeList.Add(new SelectListItem() { Value = t.Id.ToString(), Text = t.Name }); });
ViewBag.Store = storeList;
通过ViewBag给前端传值。


传值并前端绑定

<div class="form-group row"> 
    @Html.ResourceLabelFor(model => model.ProductId,  new { @class = "control-label col-md-2" })
    <div class="col-md-4">
	@Html.LocalizedDropDownListFor(model => model.ProductId,(IList<SelectListItem>)
                  ViewBag.Product, Model.ProductId,"", new { @class = "form-control" } )
	@Html.ResourceValidationMessageFor(model => model.ProductId, "", new { @class = "text-danger" })
    </div>
    @Html.ResourceLabelFor(model => model.StoreId,  new { @class = "control-label col-md-2" })
    <div class="col-md-4">
	@Html.LocalizedDropDownListFor(model => model.StoreId,(IList<SelectListItem>)ViewBag.Store, 
                  Model.StoreId,"", new { @class = "form-control" } )
	@Html.ResourceValidationMessageFor(model => model.StoreId, "", new { @class = "text-danger" })
    </div>
</div>

总结

B/S开发框架中dropdownlist控件是经常用到的,有的会绑定数据库里的值,有的会绑定枚举值,有的是界面直接固定的,在云微web开发框架中我们究竟怎么为dropdownlist下拉框绑定数据库的值呢,请跟我们一步一步来做。

网站&系统开发技术学习交流群:463167176

本站文章除注明转载外,均为本站原创或翻译,欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,共创和谐网络环境。
转载请注明:文章转载自:华晨软件-云微开发平台 » B/S开发框架_怎么为dropdownlist下拉框_绑定后台数据库值
本文标题:B/S开发框架_怎么为dropdownlist下拉框_绑定后台数据库值
本文地址:http://www.hocode.com/OrgTec/UI/0015.html

相关文章: 深入Web框架-DropDownList的常见错误

电话
电话 18718672256

扫一扫
二维码