Click here to Skip to main content
15,068,678 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

SQL
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

C#
 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...
Posted

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
C#
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
C#
ViewData["Executives"] =
                DropDownList<Executives>.LoadItems(
                    objExecutivesDbContext.Executives.ToList(), "ExecutiveId", "ExecutiveName ");

Call the viewData from the View like below
HTML
<div class="editor-field">
            @Html.DropDownListFor(model => model.ExecutiveId, (IEnumerable<SelectListItem>) ViewData["Executives"], "--Select--")
    
        </div>

Hope this helps
   
v2
Comments
Shanmugam Rathakrishnan 8-Feb-13 4:28am
   
Thanks for your reply, but i have solved it by the below solution...
In the Model... i added one more property, that is,
SQL
public virtual IEnumerable<executives> ExecutivesList { get; set; }</executives>


In the Controller...

SQL
// GET: /Clients/Create

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


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



Now its, Working Fine....
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900