0

我是 ASP .NET 的新手,尤其是 ASP .Net MVC3 Razor...

我在 MVC3 Razor 中为客户和高管创建了视图。

我所做的是第一次创建了名为 Clients.cs 的模型

namespace MVCInetClient.Model

{
    [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; }
     }
  }

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

Template : Controller With Read/Write actions and Views, using Entity Framework
Model Class : Clients (MVCInetClient.Models)
Data Context Class : ClientsDbContext (MVCInetClient.Models)

它自动创建视图创建、编辑、索引、删除,并且工作正常。

同样,我为名为 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; }
     }
 }

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

我需要的是,我需要客户视图中的主管姓名下拉列表,而不是编辑器字段。

我看了一些教程,但我很困惑...请帮我解决它...

4

1 回答 1

0

似乎您正在将 EF Code First 与 MVC 一起使用。

最简单的答案是您需要一个高管列表,将模型中的列表传递回您的视图,创建一个选择列表,然后呈现一个下拉列表。

public ActionResult Execx()
{
    Model Model = new Model();
    Model.execList = dbcontext.Executives;

    return View(Model);
}

在您看来:

@Html.DropDownListFor(model => model.execId, new SelectList(Model.execList, "ExecutiveId", "ExecutiveName "))

这是未经测试的代码。我把这些碎片拼在一起给你这个想法。

于 2013-02-07T11:48:35.060 回答