MVC Razor实体框架中的DropDown框 [英] DropDown Box in MVC Razor Entity Framework

查看:94
本文介绍了MVC Razor实体框架中的DropDown框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我是ASP .NET新手,尤其是ASP .Net MVC3 Razor ...



i为MVC3 Razor中的客户和高管创建了视图。



我做的第一个我创建的模型名为Clients.cs



命名空间MVCInetClient.Models 
{
[ tbl_Customer)]
public 类客户
{
[必需,< span class =code-keyword> Key ,DatabaseGenerated(DatabaseGeneratedOption.None)]
[显示(名称= 客户端ID)]
public int ClientId {get; set ; }
[必需]
[显示(名称= 客户名称) ]
public string ClientName {get; set ; }
[必需]
[显示(名称= 执行名称) ]
public string ExecutiveName {get; set ; }
[必需]
[显示(名称= 联系人姓名) ]
public string ContactPerson {get; set ; }
[显示(名称= 地址)]
public string Add1 {get; set ; }
[显示(名称= )]
public string Add2 {get; set ; }
[显示(名称= )]
public string Add3 {get; set ; }
[显示(名称= Pincode)]
public string Pin {get; set ; }
[显示(名称= 状态)]
public string State {get; set ; }
[显示(名称= 国家)]
public string Country {get; set ; }
[显示(名称= 电话)]
public string Phone {get; set ; }
[必需]
[StringLength( 10 )]
[RegularExpression( \\d +)]
[显示(名称= 移动)]
public string Mobile {get; set ; }
[显示(名称= 传真)]
public string Fax {get; set ; }
[显示(名称= 电子邮件)]
public string Email {get; set ; }
[显示(名称= 网站)]
public string Web {get; set ; }
}

public 类ClientsDbContext:DbContext
{
public DbSet< Clients>客户{get; set ; }
public DbSet< Executives>高管{得到; set ; }
}
}





之后我用脚手架选项创建了名为ClientsController的控制器,

模板:具有读/写操作和视图的控制器,使用实体框架

模型类:客户端(MVCInetClient.Models)

数据上下文类:ClientsDbContext(MVCInetClient .Models)



创建视图创建,编辑,索引,自动删除及其工作正常。



同样我为名为Executives.cs的模型做了



 命名空间 MVCInetClient.Models 
{
[表( tbl_Executive)]
public class 高管
{
[必需,密钥,DatabaseGenerated(DatabaseGeneratedOption) .None)]
[显示(名称= 执行ID)]
public int ExecutiveId {获取; set ; }
[必需]
[显示(名称= 执行名称) ]
public string ExecutiveName { get ; set ; }
[显示(名称= 地址)]
public string Add1 { get ; set ; }
[显示(名称= )]
public string Add2 { get ; set ; }
[显示(名称= )]
public string Add3 { get ; set ; }
[显示(名称= Pincode)]
public string Pin { get ; set ; }
[显示(名称= 状态)]
public string 状态{ get ; set ; }
[显示(名称= 国家)]
public string 国家/地区{获取; set ; }
[显示(名称= 电话)]
public string 电话{ get ; set ; }
[必需]
[StringLength( 10 )]
[RegularExpression( \\d +)]
[显示(名称= 移动)]
public string Mobile { get ; set ; }
[显示(名称= 电子邮件)]
public string 电子邮件{ get ; set ; }

}

public class ExecutivesDbContext: DbContext
{
public DbSet< Executives>高管{获取; set ; }
}
}





这在所有视图中工作正常(创建,编辑,删除)



我需要的是,我需要在客户视图而不是编辑器字段中显示执行名称的下拉列表。



i看了一些教程,但我很困惑......

请帮我解决一下......

解决方案

创建一个ClientViewModel类并重用Client类中的所有属性,还在此viewmodel类的基础上添加一个额外的属性ExecutiveId.Create Client View,然后按照以下步骤操作。



最佳实践是保持数据库实体分开并为每个视图创建一个单独的viewModel类。

为DropDownList添加一个公共类,如下所示

 < span class =code-keyword> public   static   class  DropDownList< T> 
{
public static SelectList LoadItems(IList< T> collection, string value string text)
{
return new SelectList(collection, value ,text);
}
}



从控制器调用方法如下

 ViewData [ 高管] = 
DropDownList<高管> .LoadItems(
objExecutivesDbContext .Executives.ToList(), ExecutiveId ExecutiveName);



从以下视图调用viewData

 <   div     class   =  editor-field  >  
@ Html.DropDownListFor(model => model.ExecutiveId,(IEnumerable < <跨度class =code-leadattribute> SelectListItem > )ViewData [Executives],--Select--)

< / div >



希望这有帮助


在模型中...我添加了一个属性,即

  public 虚拟IEnumerable< executive> ExecutivesList {get;  set ; }< / executive> 





在控制器中...



 //获取:/客户/创建

public ActionResult 创建()
{
//我添加的代码...
Clients model = new Clients();
model.ExecutivesList = db.Executives;
//
return 查看(模型);
}





在视图中...

  @ Html  .DropDownListFor(model => model.ExecutiveId,new SelectList(Model.ExecutivesList,  ExecutiveId  ExecutiveName),new { @ class  =   dropdown})







现在它,工作正常....


Hi all, i am new to ASP .NET and Particularly in ASP .Net MVC3 Razor...

i have created view for Clients and Executives in MVC3 Razor.

What i did is 1st i created Model called Clients.cs

namespace MVCInetClient.Models
{
  [Table("tbl_Customer")]
  public class Clients
  {
      [Required,Key,DatabaseGenerated(DatabaseGeneratedOption.None)]
      [Display(Name = "Client ID")]
      public int ClientId { get; set; }
      [Required]
      [Display(Name = "Client Name")]
      public string ClientName { get; set; }
      [Required]
      [Display(Name = "Executive Name")]
      public string ExecutiveName { get; set; }
      [Required]
      [Display(Name = "Contact Name")]
      public string ContactPerson { get; set; }
      [Display(Name = "Address")]
      public string Add1 { get; set; }
      [Display(Name = " ")]
      public string Add2 { get; set; }
      [Display(Name = " ")]
      public string Add3 { get; set; }
      [Display(Name = "Pincode")]
      public string Pin { get; set; }
      [Display(Name = "State")]
      public string State { get; set; }
      [Display(Name = "Country")]
      public string Country { get; set; }
      [Display(Name = "Phone")]
      public string Phone { get; set; }
      [Required]
      [StringLength(10)]
      [RegularExpression("\\d+")]
      [Display(Name = "Mobile")]
      public string Mobile { get; set; }
      [Display(Name = "Fax")]
      public string Fax { get; set; }
      [Display(Name = "Email")]
      public string Email { get; set; }
      [Display(Name = "Website")]
      public string Web { get; set; }
  }

  public class ClientsDbContext : DbContext
  {
      public DbSet<Clients> Clients { get; set; }
      public DbSet<Executives> Executives{ get; set; }
  }
}



After that i Created the Controller called ClientsController with Scaffolding Options,
Template : Controller With Read/Write actions and Views, using Entity Framework
Model Class : Clients (MVCInetClient.Models)
Data Context Class : ClientsDbContext (MVCInetClient.Models)

It Created View Create, Edit, Index, Delete Automatically and its working Fine too.

Similarly i did for model called Executives.cs

 namespace MVCInetClient.Models
{
    [Table("tbl_Executive")]
    public class Executives
    {
        [Required, Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
        [Display(Name = "Executive ID")]
        public int ExecutiveId { get; set; }
        [Required]
        [Display(Name = "Executive Name")]
        public string ExecutiveName { get; set; }
        [Display(Name = "Address")]
        public string Add1 { get; set; }
        [Display(Name = " ")]
        public string Add2 { get; set; }
        [Display(Name = " ")]
        public string Add3 { get; set; }
        [Display(Name = "Pincode")]
        public string Pin { get; set; }
        [Display(Name = "State")]
        public string State { get; set; }
        [Display(Name = "Country")]
        public string Country { get; set; }
        [Display(Name = "Phone")]
        public string Phone { get; set; }
        [Required]
        [StringLength(10)]
        [RegularExpression("\\d+")]
        [Display(Name = "Mobile")]
        public string Mobile { get; set; }
        [Display(Name = "Email")]
        public string Email { get; set; }

    }

    public class ExecutivesDbContext : DbContext
    {
        public DbSet<Executives> Executives { get; set; }
    }
}



and this too Working Fine in all views(create, edit, delete)

What i need is, i need a Dropdown list of Executive name in Clients View instead of editor Field.

i looked some tutorials but i am confused...
Please help me to solve it...

解决方案

Create a class ClientViewModel and reuse all the properties in Client class also add a additional property ExecutiveId.Create Client View on the basis of this viewmodel class then follow the below steps.

The best practice is to keep the database entity separate and create a separate viewModel class for each view.
Add a common class for DropDownList like below

public static class DropDownList<T>
   {
       public static SelectList LoadItems(IList<T> collection, string value, string text)
       {
           return new SelectList(collection, value, text);
       }
   }


Call the method from the controller like Below

ViewData["Executives"] =
                DropDownList<Executives>.LoadItems(
                    objExecutivesDbContext.Executives.ToList(), "ExecutiveId", "ExecutiveName ");


Call the viewData from the View like below

<div class="editor-field">
            @Html.DropDownListFor(model => model.ExecutiveId, (IEnumerable<SelectListItem>) ViewData["Executives"], "--Select--")
    
        </div>


Hope this helps


In the Model... i added one more property, that is,

public virtual IEnumerable<executives> ExecutivesList { get; set; }</executives>



In the Controller...

// GET: /Clients/Create

public ActionResult Create()
{
    // The Code i Added...
    Clients model = new Clients();
    model.ExecutivesList = db.Executives;
    //
    return View(model);
}



In the View...

@Html.DropDownListFor(model => model.ExecutiveId, new SelectList(Model.ExecutivesList, "ExecutiveId", "ExecutiveName"), new { @class = "dropdown" })




Now its, Working Fine....


这篇关于MVC Razor实体框架中的DropDown框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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