2017-01-05 53 views
0

我有一個要求,在文本和值在dropdownlist我需要一個更多的屬性。我在viewbag中添加了一個屬性,但不知道如何將其與下拉列表綁定。 控制器上的副作用綁定兩個以上的屬性,即文本和價值在下拉列表中的mvc

ViewBag.IncorporationType = imrSettings.GetIMRAttributeListbyModuleId(ModuleEnum.JURISTIC_PERSON_SUBTYPE, IMRAttributesEnum.JURISTICPERSONTYPE, (int)JURISTIC.INCORPORATION).Select(a => new ExtendedSelectedList { Value = a.Id.ToString(), Text = a.Value.ToTitleCase(), Attribute1 = a.AttributeValue1 }).OrderBy(a => a.Text); 

,並在視圖 -

@Html.DropDownListFor(model => model.personEntityMapping.EntitySubType2, ViewBag.IncorporationType as IEnumerable<ExtendedSelectedList>, "Select Incorporation Type", new { @id = "IncorporationType", @class = "form-control validate-req" }) 

我不能夠得到在HTML這個ATTRIBUTE1值與下拉列表選項。任何人都可以幫我綁定它。

+0

請找到[這](http://stackoverflow.com/a/19171957/2534646)希望這將有助於 – Curiousdev

回答

0

控制器

[HttpGet] 
      public ActionResult Test() 
      { 
       var viewModel = new CitySelectorViewModel(); 
       FillCities(viewModel); 
       return View(viewModel); 
      } 
     private void FillCities(CitySelectorViewModel viewModel) 
     { 
      viewModel.CitiesList.Add(new CityViewModel { CityId = 1, CityName = "Dublin", CountryName = "Ireland" }); 
      viewModel.CitiesList.Add(new CityViewModel { CityId = 2, CityName = "London", CountryName = "England" }); 
      viewModel.CitiesList.Add(new CityViewModel { CityId = 3, CityName = "Paris", CountryName = "France" }); 
      viewModel.CitiesList.Add(new CityViewModel { CityId = 4, CityName = "New York", CountryName = "United States" }); 
      viewModel.CitiesList.Add(new CityViewModel { CityId = 5, CityName = "Rome", CountryName = "Italy" }); 
     } 

,並在您的剃鬚刀

<select id="cityDropdown" name="@Html.NameFor(x => x.SelectedCity)"> 
       <option value=""></option> 
       @foreach (var city in Model.CitiesList) 
       { 
       <option value="@city.CityId" data-country="@city.CountryName" >@city.CityName</option> 
       } 
      </select> 
+0

是它的工作..謝謝..但如果它可能與強類型的HTML幫手下拉,那麼它將是可觀的。 –

+0

您需要按照[此鏈接](http://stackoverflow.com/a/19171957/2534646)中所述的自定義幫助器, – Curiousdev

相關問題