将所选值从模型传递给EnumDropDownListFor

时间:2016-02-03 10:45:33

标签: asp.net-mvc razor enums asp.net-mvc-5 selectlist

我对MVC很新,所以请在这里忍受我。我正在与Orchard CMS合作,并为Dynamics CRM添加了一个新模块(连接到MS Dynamics CRM Online数据库)。我正在尝试从我的数据库中提取一个值,并将其作为选定值传递给我的表单上的选择列表。该模块使用MVC版本5.1(Orchard CMS使用5.2我相信)

我可以从我的模型中的EnumDropDownListFor将下拉列表拉到我的View中,但是所选的值是默认值0.我可以从模型中的数据库中提取我的字段值,我得到正确的值100000000但是当我尝试把它们放在一起,我没有快速到达任何地方(现在感觉就像现在的日子一样)。

所以在我的模型中我有以下内容:

  public class AccountModel
    {
        [Display(Name = "Ethnicity")]
        [EnumDataType(typeof(new_Ethnicity))]
        public new_Ethnicity new_Ethnicity { get; set; }
    }

public enum new_Ethnicity : int
{

    [Display(Name = "African")]
    African = 100000009,
    [Display(Name = "Bangladeshi")]
    Bangladeshi = 100000006,
    [Display(Name = "Caribbean")]
    Caribbean = 100000008,
    [Display(Name = "Chinese")]
    Chinese = 100000011,
    [Display(Name = "Not Specified")]
    NotSpecified = 100000013,
    [Display(Name = "Other")]
    Other = 100000012,
    [Display(Name ="Other Asian")]
    OtherAsian = 100000007,
    [Display(Name = "Other Black")]
    OtherBlack = 100000010,
    [Display(Name = "Other Mixed")]
    OtherMixed = 100000004,
    [Display(Name = "Pakistani")]
    Pakistani = 100000005,
    [Display(Name = "White")]
    White = 100000000,
    [Display(Name = "White Asian")]
    WhiteAsian = 100000003,
    [Display(Name = "White African")]
    WhiteBlackAfrican = 100000002,
    [Display(Name = "White Caribbean")]
    WhiteCaribbean = 100000001
}

在我的控制器中我有:

  [Authorize]
        public ActionResult Index() {
            var crmUser = _repository.RetrieveMyInfoByIdentifier();
            if (crmUser != null) {
                var model = new AccountModel {
                    EmailAddress = crmUser.EMailAddress1,
                    FirstName = crmUser.FirstName,
                    LastName = crmUser.LastName,
                    AddressName = crmUser.Address1_Name,
                    AddressLine1 = crmUser.Address1_Line1,
                    AddressLine2 = crmUser.Address1_Line2,
                    AddressLine3 = crmUser.Address1_Line3,
                    City = crmUser.Address1_City,
                    County = crmUser.Address1_County,
                    Country = crmUser.Address1_Country,
                    PostCode = crmUser.Address1_PostalCode,
                    Telephone = crmUser.Address1_Telephone1,
                    MobilePhone = crmUser.MobilePhone,
                    JobTitle = crmUser.JobTitle,
                    BusinessPhone = crmUser.Telephone1,
                    MiddleName = crmUser.MiddleName,
                    EthnicOrigin = (Int32)crmUser.new_EthnicOrigin,
                    BirthDate = (DateTime)crmUser.BirthDate
                    //new_Ethnicity = crmUser.new_EthnicOrigin
                    //EthnicOriginList = new SelectList(crmUser.new_EthnicOrigin, "new_ethnicorigin", "new_ethnicorigin") 
                    //new_Ethnicity = (int)crmUser.EthnicOrigin
                    //new_contact_new_ethnicorigin = crmUser.new_EthnicOrigin
                };

                //model.BirthDate = (DateTime)crmUser.BirthDate;

                return View(model);
            }

在我的视图中我有这个:

 @Html.EnumDropDownListFor(m => m.new_Ethnicity, new { style = "width:100%;" })

我只显示了我的模型中的“必要”信息,但如果您需要查看更多信息,请询问。我还在Controller中的数据库中包含了我正在调用的所有字段,但是我已经注释了一些行,因为我已经尝试了这些方法来获取值。

我的模型中的EthnicOrigin字段确实从我的数据库100000000中获取了正确的值,但每次我尝试将正确的选定值设置到下拉列表时我的页面都没有加载(我没有错误消息,我担心这是在Orchard CMS内部构建的,这只是告诉我在看起来有错误的时候找不到页面)...所以这让我感到很沮丧。

希望我只是遗漏了一些非常非常简单的东西,有人可以告诉我如何使用我的数据库中的值设置选择列表。

Thanx家伙

0 个答案:

没有答案