微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用以下详细信息创建 Asp.Net Web 应用程序:

如何解决使用以下详细信息创建 Asp.Net Web 应用程序:

我有一个问题,那就是我做了一个 List 并将 ValueText 放入其中,但问题是当我尝试从List 它只从列表中获取 ViewBag 并存储它,但我无法通过从 Value 调用 Text获取 List。这是我的代码

ViewBag 类(模型):

Department

using System.ComponentModel.DataAnnotations; using System.Web.Mvc; namespace Project_List.Models { public class Department { public int DepartmentID { get; set; } [display(Name = "Select Department")] public string Name { get; set; } public List<SelectListItem> getDepartmentList() { return new List<SelectListItem> { new SelectListItem { Value = "1",Text = "Marketing"},new SelectListItem { Value = "2",Text = "Finance" },new SelectListItem { Value = "3",Text = "Operations Management" },new SelectListItem { Value = "4",Text = "IT" },new SelectListItem { Value = "5",Text = "Human Resource" } }; } public Employee employee { get; set; } } } 控制器代码

Employee

使用 (ViewBag) 调用 [HttpPost] public ActionResult Create(Employee employee,Department department) { //In case of Invalid user redirect to login if (Session["login"] == null) { return RedirectToAction("Login","User"); } if (ModelState.IsValid) { EmployeeEntity entity = new EmployeeEntity(); DepartmentEntity entity1 = new DepartmentEntity(); int count = entity.insert(employee,department); int count1 = entity1.insert(employee,department); if (count > 0 && count1 > 0) { ViewBag.successMessage = "Data insterted Successfully !"; } ModelState.Clear(); } ViewBag.DepartmentsList = getDepartmentList(); return View(); } private List<SelectListItem> getDepartmentList() { List<SelectListItem> departmentList = new Department().getDepartmentList(); return departmentList; }

List

ViewBag.DepartmentsList = getDepartmentList(); 视图:

Create

现在的问题是,每当我尝试将以下数据保存到数据库中时,它只会在数据库中存储 <!DOCTYPE html> <html> <head> <Meta name="viewport" content="width=device-width" /> <link href="~/Content/bootstrap.min.css" rel="stylesheet" /> <title>Employee information</title> </head> <body> @using (Html.BeginForm("Create","Employee")) { <div class="h4"> @Html.ActionLink("Back to Main Page","Index") </div> <div class="form-horizontal"> <h4 class="h1 text-center">Employee information</h4> <hr /> <div class="form-group"> @Html.LabelFor(model => model.EmployeeID,htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.EmployeeID,new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.EmployeeID,"",new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.FirstName,htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.FirstName,new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.FirstName,new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.LastName,htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.LastName,new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.LastName,new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Gender,htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Gender,new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Gender,new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.EmailAddress,htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.EmailAddress,new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.EmailAddress,new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.City,htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.City,new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.City,new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Salary,htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Salary,new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Salary,new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Age,htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Age,new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Age,new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.EducationLevel,htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.EducationLevel,new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.EducationLevel,new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.HireDate,htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.HireDate,new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.HireDate,new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.department.Name,htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.DropDownListFor(x => x.department.DepartmentID,(List<SelectListItem>)ViewBag.DepartmentsList) </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="Create" class="btn btndefault" /> </div> </div> </div> } </body> </html> 而不是 Value,这里是插入数据库代码

Text 类(模型):

Employee

public class Employee { public int EmployeeID { get; set; } [required] public string FirstName { get; set; } public string LastName { get; set; } [required] public string Gender { get; set; } [required] [Range(20,60,ErrorMessage = "Age must be between 20 and 60")] [display(Name = "Age")] public int Age { get; set; } [required] [display(Name = "Education Level")] public int EducationLevel { get; set; } [Range(25000,500000,ErrorMessage = "Please enter correct value")] [required] /* We can control the display of data in a View (UI) using display attributes */ [display(Name = "Salary")] public int Salary { get; set; } [required] [EmailAddress] public string EmailAddress { get; set; } [required(ErrorMessage = "Please enter hire date")] [display(Name = "Hire Date")] [CustomHireDate(ErrorMessage = "Hire Date must be less than or equal to Today's Date")] [DataType(DataType.Date)] public DateTime? HireDate { get; set; } public string City { get; set; } public Department department { get; set; } } (类):

EmployeeEntity

string ConnectionString = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString; sqlConnection sqlConnection = null; sqlCommand cmd = null; public int insert(Employee employee,Department department) { int effectedRows = 0; try { sqlConnection = new sqlConnection(ConnectionString); string query = @"insert into Employee(EmployeeID,FirstName,LastName,Gender,Age,EducationLevel,Salary,EmailAddress,HireDate,City,DepartmentID) values('" + employee.EmployeeID + "','" + employee.FirstName + "','" + employee.LastName + "','" + employee.Gender + "','" + employee.Age + "','" + employee.EducationLevel + "','" + employee.Salary + "','" + employee.EmailAddress + "','" + employee.HireDate + "','" + employee.City + "','" + employee.department.DepartmentID + "')"; sqlConnection.open(); cmd = new sqlCommand(query,sqlConnection); effectedRows = cmd.ExecuteNonQuery(); sqlConnection.Close(); return effectedRows; } catch (Exception exp) { return effectedRows; } } (类):

DepartmentEntity

数据库中插入输出后:

数据库 ( string ConnectionString = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString; sqlConnection sqlConnection = null; sqlCommand cmd = null; public int insert(Employee employee,Department department) { int effectedRows = 0; try { sqlConnection = new sqlConnection(ConnectionString); string query = @"insert into Department(DepartmentID,Name) values('" + employee.department.DepartmentID + "','" + department.Name + "')"; sqlConnection.open(); cmd = new sqlCommand(query,sqlConnection); effectedRows = cmd.ExecuteNonQuery(); sqlConnection.Close(); return effectedRows; } catch (Exception exp) { return effectedRows; } } ) 输出

enter image description here

数据库 (Employee) 输出

enter image description here

正如您在 Department 表的输出中所见,当我尝试插入 Department 表时,名称未从列表中存储。我无法弄清楚这个问题,感谢任何可以解决这个问题的帮助。

数据库Department 表):

enter image description here

数据库Employee 表):

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。