Populate a DropdownList with composite key records in MVC3.net?

折月煮酒 提交于 2019-12-13 18:17:58

问题


I don't know if I'm missing something obvious, but I really want to grab names of clients associated with a composite key.

Controller Code:

Job job = db.Jobs.Find(id);


            ViewBag.jobClientsList = new SelectList(job.JobClients.ToList(), "ClientNumber", "ClientNumber");

View Code:

<%: Html.DropDownList("ClientNumber", ViewData["JobClientsList"] as SelectList)%>

Model:

namespace Sample.CustomerService.Domain {
using System.ComponentModel.DataAnnotations;

public class JobClient {
    public JobClient() { }
    [Key]
    [Column(Order = 1)]
    public virtual int JobNumber { get; set; }
    [Key]
    [Column(Order = 1)]
    public virtual int ClientNumber { get; set; }
    public virtual Client Client { get; set; }
    public virtual Job Job { get; set; }
}

}

This code works, but all I get in the dropdownlist is a bunch of numbers. What I would really like is the client names associated with the numbers but I'm really not sure how to do it! I've been looking around for ages!


回答1:


After re-reading your question your answer seems simpler then expected. Check out the Select list class http://msdn.microsoft.com/en-us/library/system.web.mvc.selectlist.aspx

The constructor your using in your controller is wrong, it should be:

ViewBag.jobClientsList = new SelectList(job.JobClients.ToList(), "ClientNumber", "Client");

You were setting the text value of the selectList to be "ClientNumber" which is why you had a list of numbers and not names!




回答2:


By default the select list is showing you the property that is marked [Key]

<%: Html.DropDownList("ClientNumber", ViewData["JobClientsList"].Client as SelectList)%>

Should print the client name (assuming the primary Key on the Client object is their name, otherwise You'd need something like ViewData["JobClientsList"].Client.FullName

The best solution would be to use a ViewModel instead of using ViewBag or ViewData for this, it'll help avoid a lot of headaches both now and in the future.




回答3:


What I have done in the past to get DropDownLists working is save the List to the Session Variable, and then create my SelectList in the actual DropDownList.
Controller:

Job job = db.Jobs.Find(id).ToList();  
ViewBag.jobClientList = job;  

View:

<%: Html.DropDownList("ClientNumber", new SelectList((If you view is strongly typed, put that here) ViewData["JobClientsList"],"ClientNumber","ClientNumber")%>

This may be poorly worded, so I think I can clarify if need be




回答4:


Anyone looking for a solution, try the following:

In your controller:

1) Get the list:

var allCountries = countryRepository.GetAllCountries();

2) define a variable:

var items = new List<SelectListItem>();

3)loop each item:

foreach(var country in allCountries)
 {
    items.Add(new SelectListItem() {
    Text = coutry.Name,
    Value = Country.Id.ToString(),
    // Put all sorts of business logic in here
    Selected = country.Id == 13 ? true : false
 });
}

model.Countries = items;

In your view:

@Html.ActionLink("StringToDisplay", "actionName", "controllerName", new SelectList(Model.Countries, "Value","Text"),"--Please Select--", new{@class="form-control"})

Not to mention Model should have a property with

public IEnumerable<Country> Countries {get; set;}


来源:https://stackoverflow.com/questions/7688744/populate-a-dropdownlist-with-composite-key-records-in-mvc3-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!