Matt Butler

Matt Butler

  • NA
  • 18
  • 0

MVC3 - Select Lists & Items

Apr 12 2013 11:18 PM
I am trying to build a selectlist in my view where the user can select the company name they want to schedule the job under. Since the Customer model houses all the customer data (address, phone, local contact) I don't need to do anything more than select the proper customer. As you can see below, my job model will take a customer object as part of that model. What I want to do is use the CompanyName field in the customer model, but return the ID field from the customer model to my controller.

I have tried multiple tutorials on the internet and haven't gotten any where.

Customer Model:

public int ID { get; set; }

[Required]
[Display(Name = "Company Name")]
[StringLength(255)]
public virtual string CompanyName { get; set; }
...

Job Model:
...
public Customer Customer { get; set; }
public Technician Technician { get; set; }

I know the selectlist will take a value and a text property. However, I am having a hard time passing those two pieces of information to my view. Anybody have any suggestions.

The last thing I can think of is to just list out the company names and then do a search function via LINQ to find the ID in the controller. Obviously, this would be a horrible way to go about it and it would result in extra processing that doesn't need to occur. Any ideas are much appreciated.