如何在mvc3中保存下拉列表项 [英] how to save dropdownlist item in mvc3

查看:120
本文介绍了如何在mvc3中保存下拉列表项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个与三个数据库表相关的类文件。

员工

部门

指定



Department and Designation表与使用外键Key DeptID和DesID的employee表相关。



我的问题是,我想要'ReportingTo'字段编辑视图应该通过同一员工类属性EmployeeName中的DropDownList项填充。



ViewBag.DeptID和ViewBag.DesID由EntityFramework在创建控制器类时自动生成我添加了ViewBag.EmpNo



以下是我所做的代码片段及其工作情况。但是从下拉列表中选择一个项目时,值不会被保存在数据库中,因此输出字段为空。



如何保存所选项目以便在查看页面上显示?



谢谢。



EmployeeController.cs

  //   GET:/ Employee / Edit / 5  

public ActionResult Edit( int id)
{
Employee employee = db.Employees.Find(id);
ViewBag.DeptID = new SelectList(db.Departments, DeptID DeptShortDesc,employee.DeptID);
ViewBag.DesID = new SelectList(db.Designations, DesID ShortDesc,employee.DesID);
ViewBag.EmpNo = new SelectList(db.Employees, EmpNo EmployeeName,employee.EmpNo);

}


// POST:/ Employee / Edit / 5

[HttpPost]
public ActionResult Edit(员工雇员)
{
if (ModelState.IsValid)
{
db.Entry(employee).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction( Index);
}
ViewBag.DeptID = new SelectList(db.Departments, < span class =code-string> DeptID
DeptShortDesc,员工。 DEPTID);
ViewBag.DesID = new SelectList(db.Designations, DesID ShortDesc,employee.DesID);
ViewBag.EmpNo = new SelectList(db.Employees, EmpNo EmployeeName,employee.EmpNo);

}



Edit.cshtml文件

< div  class  =   editor-label >  
@ Html.LabelFor(model = > model.EmpNo, ReportingTo
< / div >
< div class = editor-field >
@ Html.DropDownList( EmpNo字符串 .Empty)
@ Html.ValidationMessageFor(model = > 模式l.EmpNo)
< / div >

解决方案

型号类:



< pre lang =c#> 命名空间 Emp1_MVC.Models
{
使用系统;
使用 System.Collections.Generic;

public partial class Emp1MVC
{
public int EmpID {获得; set ; }
public string EmpName { get ; set ; }
public string 城市{ get ; set ; }
}
}



控制器:



  public   class  HomeController:Controller 
{
//
// GET:/ Home /
ASPEntities dc = new ASPEntities();
private 列表< SelectListItem> GetCities()
{
List< SelectListItem> cities = new 列表< SelectListItem>();
cities.Add( new SelectListItem {Value = 1,Text = 德里});
cities.Add( new SelectListItem {Value = 2,Text = Pune});
cities.Add( new SelectListItem {Value = 3,Text = Gurgaon});
cities.Add( new SelectListItem {Value = 4,Text = Noida});
cities.Add( new SelectListItem {Value = 5,Text = 班加罗尔});
cities.Add( new SelectListItem {Value = 6,Text = Chennai});
cities.Add( new SelectListItem {Value = 7,Text = Hyderabad});
返回城市;
}
[HttpGet]
public ActionResult Third()
{
ViewBag.citiesddl = GetCities( );
return View();
}
[HttpPost]
public ActionResult Third(Emp1MVC emp)
{
return 查看(emp);
}
}



查看文件:



 @model Emp1_MVC.Models.Emp1MVC 
@ {
ViewBag.Title =Third;
}
< h2 > 第三个< / h2 > ;
@using(Html.BeginForm())
{
@ Html.Label(lblEmpID,员工ID);
@ MvcHtmlString.Create();
@ MvcHtmlString.Create();
@ MvcHtmlString.Create();
@ MvcHtmlString.Create();
@ MvcHtmlString.Create();
@ MvcHtmlString.Create();
@ Html.TextBoxFor(c => c.EmpID);
< br < span class =code-keyword> / >
< br / >
@ Html.Label(lblEmpName,员工姓名);
@ MvcHtmlString.Create();
@ Html.TextBoxFor(c => c.EmpName);
< br < span class =code-keyword> / >
< br / >
@ Html.Label(lblCCity,员工城市);
@ MvcHtmlString.Create();
@ MvcHtmlString.Create();
@ MvcHtmlString.Create();
@ MvcHtmlString.Create();
@Html.DropDownList(ddlCity,新的SelectList(ViewBag.citiesddl,Value,Text),---- Select City ----);
< br < span class =code-keyword> / >
< br / >
< 输入 类型 = 提交 value = 添加 / >
}


I have three Class files relevant to three Database Tables.
Employee
Department
Designation

Department and Designation tables are related to employee table using foreign Key DeptID and DesID.

My question is,I want 'ReportingTo" field in Edit View should get populated through DropDownList item from the same employee class property "EmployeeName".

ViewBag.DeptID and ViewBag.DesID are generated automatically by EntityFramework while creating controller class and I have added ViewBag.EmpNo

Below is the code snippet what I did.and its working. but on selecting an item from the dropdownlist,value do not get saved in database,hence the output field is blank.

How Can I save the selected Item so that it could be seen on View Page ?

Thank you.

EmployeeController.cs

// GET: /Employee/Edit/5
 
        public ActionResult Edit(int id)
        {
            Employee employee = db.Employees.Find(id);
            ViewBag.DeptID = new SelectList(db.Departments, "DeptID", "DeptShortDesc", employee.DeptID);
            ViewBag.DesID = new SelectList(db.Designations, "DesID", "ShortDesc", employee.DesID);
            ViewBag.EmpNo = new SelectList(db.Employees, "EmpNo", "EmployeeName", employee.EmpNo);
           
        }

        
        // POST: /Employee/Edit/5

        [HttpPost]
        public ActionResult Edit(Employee employee)
        {
            if (ModelState.IsValid)
            {
                db.Entry(employee).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.DeptID = new SelectList(db.Departments, "DeptID", "DeptShortDesc", employee.DeptID);
            ViewBag.DesID = new SelectList(db.Designations, "DesID", "ShortDesc", employee.DesID);
            ViewBag.EmpNo = new SelectList(db.Employees, "EmpNo", "EmployeeName", employee.EmpNo);
          
        }


Edit.cshtml file

<div class="editor-label">
            @Html.LabelFor(model => model.EmpNo,"ReportingTo")
        </div>
        <div class="editor-field">
            @Html.DropDownList("EmpNo", String.Empty)
            @Html.ValidationMessageFor(model => model.EmpNo)
        </div>

解决方案

Model class:

namespace Emp1_MVC.Models
{
using System;
using System.Collections.Generic;

public partial class Emp1MVC
{
public int EmpID { get; set; }
public string EmpName { get; set; }
public string City { get; set; }
}
}


controller:

public class HomeController : Controller
{
//
// GET: /Home/
ASPEntities dc = new ASPEntities();
private List<SelectListItem> GetCities()
{
List<SelectListItem> cities = new List<SelectListItem>();
cities.Add(new SelectListItem { Value = "1", Text = "Delhi" });
cities.Add(new SelectListItem { Value = "2", Text = "Pune" });
cities.Add(new SelectListItem { Value = "3", Text = "Gurgaon" });
cities.Add(new SelectListItem { Value = "4", Text = "Noida" });
cities.Add(new SelectListItem { Value = "5", Text = "Bangalore" });
cities.Add(new SelectListItem { Value = "6", Text = "Chennai" });
cities.Add(new SelectListItem { Value = "7", Text = "Hyderabad" });
return cities;
}
[HttpGet]
public ActionResult Third()
{ 
ViewBag.citiesddl = GetCities();
return View();
}
[HttpPost]
public ActionResult Third(Emp1MVC emp)
{
return View(emp);
}
}


View File :

@model Emp1_MVC.Models.Emp1MVC
@{
ViewBag.Title = "Third";
}
<h2>Third</h2>
@using (Html.BeginForm())
{
@Html.Label("lblEmpID", "Employee ID");
@MvcHtmlString.Create(" ");
@MvcHtmlString.Create(" ");
@MvcHtmlString.Create(" ");
@MvcHtmlString.Create(" ");
@MvcHtmlString.Create(" ");
@MvcHtmlString.Create(" ");
@Html.TextBoxFor(c => c.EmpID);
<br />
<br />
@Html.Label("lblEmpName", "Employee Name");
@MvcHtmlString.Create(" ");
@Html.TextBoxFor(c => c.EmpName);
<br />
<br />
@Html.Label("lblCCity", "Employee City");
@MvcHtmlString.Create(" ");
@MvcHtmlString.Create(" ");
@MvcHtmlString.Create(" ");
@MvcHtmlString.Create(" ");
@Html.DropDownList("ddlCity", new SelectList(ViewBag.citiesddl, "Value", "Text"), "----Select City----");
<br />
<br />
<input type="submit" value="Add" />
}


这篇关于如何在mvc3中保存下拉列表项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆