无法将类型'System.Collections.Generic.List <system.web.mvc.selectlistitem>'隐式转换为'int?'</system.web.mvc.selectlistitem>

时间:2012-04-12 14:48:28

标签: asp.net-mvc asp.net-mvc-3

在mvc3应用程序中我正在使用以下查询从数据库填充下拉列表。 在Index.cshtml上获得错误

public mydataEntities1 dbContext = new mydataEntities1();

        public List<SelectListItem> GetPricingSecurityID()
        {
        var pricingSecurityID = (from m in dbContext.Reporting_DailyNAV_Pricing
                                     select new SelectListItem
                                         {
                                                Text = m.PricingSecurityID.ToString(),
                                                Value = m.PricingSecurityID.ToString()
                                         });

        return pricingSecurityID.ToList();
        }

        public List<SelectListItem> GetCUSIP()
        {
            var cusipID = (from m in dbContext.StateStreet_DailyPosition_Second
                                     select new SelectListItem
                                     {
                                         Text = m.CUSIP.ToString(),
                                         Value = m.CUSIP.ToString()
                                     });

            return cusipID.ToList();
        }

Home.controller
public ActionResult Index()
        {
            SecurityIdentifierMapping objModel = new SecurityIdentifierMapping();
            objModel.PricingSecurityID = objRepository.GetPricingSecurityID();
            objModel.CUSIP = objRepository.GetCUSIP();
            return View(objModel);
        }

public partial class SecurityIdentifierMapping
    {
        public int Id { get; set; }

        [Required(ErrorMessage = "Please select a PricingSecurityID")]
        public Nullable<int> PricingSecurityID { get; set; }

        [Required(ErrorMessage = "Please select a CUSIPID")]
        public string CUSIP { get; set; }

        public string Calculation { get; set; }
    }

尝试AsEnumerable()还要改变什么?

1 个答案:

答案 0 :(得分:1)

如果没有看到SecurityIdentiferMapping的定义,我无法确定,但我猜测PricingSecurityID的类型为int?。您正尝试将GetPricingSecurityID()的返回值设置为此属性,但该方法的返回值为List<SelectItemList>

更改GetPricingSecurityID()方法以返回int?或您的财产以接受List<SelectItemList>