分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 教程案例

@Html.DropDownListFor 下拉框绑定(选择默认值)

发布时间:2023-09-06 02:32责任编辑:白小东关键词:暂无标签

首先先构建绑定下拉框的数据源

 ?private void GetSalesList() ???????{ ???????????var userList = _rmaExpressAppService.GetUserList(); ???????????TempData["RMASalesList"] = new SelectList(userList, "Id", "UserName");//选择 userList中的Id作为 Value,选择·UserName 作为Text显示 ???????}

然后构建前台,推荐用Html.DropDownListFor ,   model.Model.SaleExecutor 既可以在选择的时候将对应的Value传回去,也可以在初始化的时候给字段赋默认值

 ??????????@Html.DropDownListFor(model => model.Model.SaleExecutor, TempData["RMASalesList"] as SelectList, new { @class = "form-control" })

注意初始化的时候要先赋值给前台

 ??/// <summary> ???????/// 新增快递单-初始空页面 ???????/// </summary> ???????[HttpGet] ???????[AccessGroup(GroupKey = "RMAExpress_Add")] ???????public ActionResult Add(RMAExpressDetailInfo model) ???????{ ???????????GetSalesList();//为前台下拉框绑定值做准备 ???????????model.Model.SaleExecutor = userId;//传默认值给前台 ???????????return View(model); ???????}

在提交的时候,会自动把值映射到对应的模型中,当然特殊需要的时候可以通过Jquery单独获取选中值

 ?var saleUserId = $("#Model_SaleExecutor").find("option:selected").val();//找到下拉框的选中的Value

@Html.DropDownListFor 下拉框绑定(选择默认值)

原文地址:https://www.cnblogs.com/wang-min/p/10338609.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved