ASP.NETMVC中动态下拉列表框的实现问题,不能使用ActionResultCreate()方法实现。
创始人
2024-09-19 09:01:10
0

下面是一个使用JsonResult来返回动态下拉列表数据的示例。 首先,创建一个模型类,比如Employee,其中包含需要的属性,以及与其他模型类的关系(如果有)。

public class Employee { public int EmployeeId { get; set; } public string Name { get; set; } public int DepartmentId { get; set; } public Department Department { get; set; } }

然后,创建一个方法在控制器中来获取下拉列表数据,以便在视图中使用。

public JsonResult GetDepartmentList() { var departments = db.Departments.ToList(); var departmentList = departments.Select(d => new SelectListItem { Value = d.DepartmentId.ToString(), Text = d.Name }).ToList(); return Json(departmentList, JsonRequestBehavior.AllowGet); }

在视图中,使用Ajax来异步获取下拉列表数据,在下拉列表中使用JavaScript来绑定数据。

@{ ViewBag.Title = "Create"; }

Create

@using (Html.BeginForm()) { @Html.AntiForgeryToken()

Employee


@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
@Html.LabelFor(model => model.DepartmentId, htmlAttributes: new { @class = "control-label col-md-2" })

相关内容

热门资讯

透视了解!aapoker脚本怎... 透视了解!aapoker脚本怎么用,智星德州辅助译码插件靠谱吗,大家学习交流(有挂教学)1、很好的工...
透视游戏!wepoker辅助下... 透视游戏!wepoker辅助下载,aapoker透视插件,解密教程(其实存在有挂);值得一提的是,a...
透视脚本!wepoker透视脚... 您好,aapoker辅助插件工具这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】...
透视ai!hhpoker怎么开... 透视ai!hhpoker怎么开透视,哈糖大菠萝攻略,玩家教程(其实有挂);一、hhpoker怎么开透...
透视好友房!hhpoker辅助... 透视好友房!hhpoker辅助软件下载,智星德州菠萝插件官网,玩家必看(有挂黑科技)是一款可以让一直...
透视有挂!德普之星透视辅助,w... 透视有挂!德普之星透视辅助,wepoker开辅助能查到吗,新2025版(确实真的有挂);建议优先通过...
透视黑科技!hhpoker透视... 您好,hhpoker透视脚本视频这款游戏可以开挂的,确实是有挂的,需要了解加Q群【106723914...
透视透视!aapoker可以开... 透视透视!aapoker可以开挂吗,aapoker怎么提高中牌率,解密教程(原本真的有挂);aapo...
透视免费!wepoker辅助器... 1、透视免费!wepoker辅助器下载,德普之星辅助功能如何打开,玩家必看分享(有挂技巧);代表性(...
透视辅助!智星德州菠萝插件官网... 透视辅助!智星德州菠萝插件官网,wepoker私人局辅助挂,安装教程(总是真的有挂);建议优先通过智...