Click here to Skip to main content
12,406,795 members (62,129 online)
Rate this:
 
Please Sign up or sign in to vote.
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...
Posted 6-Feb-13 22:55pm
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
v2
Comments
arshanmugam 8-Feb-13 4:28am
   
Thanks for your reply, but i have solved it by the below solution...
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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....
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160730.1 | Last Updated 8 Feb 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100