我很困惑如何有效地使用ASP.NET MVC的Html.DropDownList助手。DropDownList不能在ASP.Net中工作100%MVC
背景:我有一個5頁的表單,每次單擊「下一步」時將數據保存到表單中。用戶可以在部分之間來回導航,因此以前的部分將已經預填充了先前輸入的數據。
這適用於TextBoxes。但不是DropDownLists。我曾嘗試不同的方法的負載,包括:
- How to add static list of items in MVC Html.DropDownList()
- Setting selected item to DropdownList in MVC Application?
我有一個視圖模型等taht我有我的名單,我的模型(生成的LINQ到SQL類)作爲屬性。例如:
public class ConsultantRegistrationFormViewModel
{
public IConsultantRegistration ConsultantRegistration { get; private set; }
public SelectList Titles { get; private set; }
public SelectList Countries { get; private set; }
public SelectList Currencies { get; private set; }
public int CurrentSection { get; private set; }
private ConsultantRegistrationFormViewModel(IConsultantRegistration consultantRegistration)
{
ConsultantRegistration = consultantRegistration;
CurrentSection = 1;
Titles = new SelectList(new string[] { "Mr", "Mrs", "Miss", "Ms", "Dr", "Sir" });
Countries = new SelectList(countries.Select(q => q.Name));
Currencies = new SelectList(currencies,"CurrencyCode","FriendlyForm");
}
}
上得到我的控制器的編輯操作是這樣的:
public class ConsultantRegistrationController : Controller
{
public IConsultantRegistrationRepository ConsultantRegistrationRepository { get; private set; }
public ICountryRepository CountryRepository { get; private set; }
public IEnumerable<ICountry> Countries { get; private set; }
public ConsultantRegistrationController()
{
ConsultantRegistrationRepository = RepositoryFactory.CreateConsultantRegistrationRepository();
CountryRepository = RepositoryFactory.CreateCountryRepository();
Countries = CountryRepository.GetCountries().ToArray();
}
public ActionResult Edit(Guid id, int sectionIndex)
{
IConsultantRegistration consultantRegistration = ConsultantRegistrationRepository.GetConsultantRegistration(id);
SelectList bankBranchCountriesSelectList = new SelectList(Countries, "BankBranchCountry", "CountryName", consultantRegistration.BankBranchCountry);
ViewData["bankBranchCountrySelectList"] = bankBranchCountriesSelectList;
return View(new ConsultantRegistrationFormViewModel(consultantRegistration,sectionIndex, Countries,Currencies));
}
}
我的觀點做:
<%: Html.DropDownList("ConsultantRegistration.BankBranchCountry",ViewData["bankBranchCountrySelectList"] as SelectList) %>
這給我的錯誤:
DataBinding: 'IWW.ArrowPay.ConsultantRegistration.Data.Country' does not contain a property with the name 'BankBranchCountry'.
它所做的,看看架構此屬性的:
public interface IConsultantRegistration
{
Guid ID { get; set; }
[DisplayName("Branch Country")]
string BankBranchCountry { get; set; }
}
(我的LINQ到SQL類型ConsultantRegistration實現IConsultantRegistration)
看來,它正試圖綁定到錯誤類型有關係嗎?
如果我用這個在我看來(和使用我的控制器的國家屬性):
<%: Html.DropDownList("ConsultantRegistration.BankBranchCountry ",Model.Countries,"(select a Country)") %>
我得到保存的值很好,但我的模型不會在之後的更新。
如果我用這個在我看來:在我的視圖模型
<%: Html.DropDownListFor(model=>model.ConsultantRegistration.BankBranchCountry ",Model.Countries,"(select a Country)") %>
我得到的名單,並將其職位所選擇的值改回,但不預先選擇當前選定的項目。
所以我有一個解決方案遍佈各地,但不是全部在一個地方。
希望你能幫助填補我的無知。
你能爲下拉菜單顯示呈現的HTML嗎? – 2010-04-09 11:51:57
謝謝馬爾科姆,抱歉,我現在不能。我太過於剽竊它,無法使用它。無論如何,它已經有了工作。 – 2010-04-09 14:44:24