无法在MVC下拉列表中绑定数据库中的项目。请帮忙 [英] Not able to bind items from database in MVC dropdownlist. Please help
问题描述
我正在尝试从sql server数据库中绑定mvc中dropdownlist中的部门。我为此创建了模型,控制器和视图。该项目运行文件没有任何错误,但仍然没有填写我的下拉列表中的数据库部门。
以下是数据库脚本和代码
我有一个名为Dbemp的数据库,我有一个表部门如下图所示srcipt
< pre lang =SQL> USE [Dbemp]
GO
创建 表 [dbo]。[部门](
[DeptId] [ int ] IDENTITY ( 1 , 1 ) NOT NULL ,
[DeptName] [ varchar ]( 50 ) NOT < span class =code-keyword> NULL ,
PRIMARY KEY CLUSTERED
(
[DeptId] ASC
) WITH (PAD_INDEX = OFF ,STATISTICS_NORECOMPUTE = OFF ,IGNORE_DUP_KEY = OFF ,ALLOW_ROW_LOCKS = ON ,ALLOW_PAGE_LOCKS = ON ) ON [ PRIMARY ]
) ON [ PRIMARY ]
GO
SET ANSI_PADDING OFF
GO
我的部门模型
使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.ComponentModel.DataAnnotations;
namespace DynamicDropDownListMVC.Models
{
public class 部门
{
[Key]
public int DeptId { get ; set ; }
public string DeptName { get 跨度>; set ; }
}
}
web.config文件中的我的连接字符串
< connectionstrings >
< add 名称 = MyDBContext < span class =code-attribute> connectionstring = 数据源=我的PC;初始目录= Dbemp;集成安全性=真 providername = System.Data.SqlClient / >
< / connectionstrings > ;
我的控制器
使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Web.Mvc;
使用 DynamicDropDownListMVC.Models;
namespace DynamicDropDownListMVC.Controllers
{
public class HomeController:Controller
{
public ActionResult Index()
{
MyDBContext db = new MyDBContext();
var data = db.Departments.ToList();
ViewBag.Departments = new SelectList(db.Departments, DeptId, DeptName);
return View();
}
}
}
我的观点:
@ {
ViewBag.Title =主页;
}
@ Html.DropDownList(部门,选择部门)
我认为你编码的方式是
@ Html.DropDownList(部门,选择部门)
错了
尝试使用如下所示,
@ Html.DropDownListFor(m => m.name,(SelectList) )ViewBag.DropdownListOptions)
这是因为你需要指定模型属性,这里我提到m => m.name用于展示一个例子。你需要写m => m.departments。
我在这里看到你没有使用任何viewmodel。你最好在这里使用viewmodel。
点击此处以便更好地理解 [ ^ ]
我希望我的话能帮到你。
谢谢
从数据库中看到本文绑定@ Html.DropdownListFor()
http://www.c-sharpcorner .com / UploadFile / 4d9083 / binding-dropdownlist-in-mvc-in-various-ways-in-mvc-with-data / [ ^ ]
http://www.c-sharpcorner.com/UploadFile/4d9083/creating-simple-cascading-dropdownlist -in-mvc-4-using-razor / [ ^ ]
我的问题通过以下文章解决:
使用实体框架从Sql Server数据库动态绑定Asp.Net MVC下拉列表
I am trying to bind departments in dropdownlist in mvc from sql server database. I have created model, controller and view for that. The project is running file without any error but still it is not filling the departments from the database in my dropdownlist.
Below are the database script and the code
I have a database named "Dbemp" and in that I have a table Department as shown below srcipt
USE [Dbemp]
GO
CREATE TABLE [dbo].[Department](
[DeptId] [int] IDENTITY(1,1) NOT NULL,
[DeptName] [varchar](50) NOT NULL,
PRIMARY KEY CLUSTERED
(
[DeptId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
My Department Model
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace DynamicDropDownListMVC.Models
{
public class Department
{
[Key]
public int DeptId { get; set; }
public string DeptName { get; set; }
}
}
My connectionstring in web.config file
<connectionstrings>
<add name="MyDBContext" connectionstring="Data Source=My-PC;Initial Catalog=Dbemp;Integrated Security=True" providername="System.Data.SqlClient" />
</connectionstrings>
My Controller
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DynamicDropDownListMVC.Models;
namespace DynamicDropDownListMVC.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
MyDBContext db = new MyDBContext();
var data = db.Departments.ToList();
ViewBag.Departments = new SelectList(db.Departments, "DeptId", "DeptName");
return View();
}
}
}
My View:
@{
ViewBag.Title = "Home Page";
}
@Html.DropDownList("Departments", "Select Department")
I think the way you have coded the
@Html.DropDownList("Departments", "Select Department")
Is wrong
Try using like below,
@Html.DropDownListFor(m=>m.name, (SelectList)ViewBag.DropdownListOptions)
This is because you need to specify the model property, here i have mentioned m=>m.name for showing an example. You need to write m=>m.departments.
I see here you have not used any viewmodel. It is better you use viewmodel here.
Check here for better understanding[^]
I hope my words help you.
Thanks
see this article for binding @Html.DropdownListFor() from data base
http://www.c-sharpcorner.com/UploadFile/4d9083/binding-dropdownlist-in-mvc-in-various-ways-in-mvc-with-data/[^]
http://www.c-sharpcorner.com/UploadFile/4d9083/creating-simple-cascading-dropdownlist-in-mvc-4-using-razor/[^]
My problem was solved by the article:
Dynamically bind Asp.Net MVC Dropdownlist from Sql Server Database using entity framework
这篇关于无法在MVC下拉列表中绑定数据库中的项目。请帮忙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!