我对 MVC 很陌生。我试图用从数据库中检索到的货币填充下拉列表。我究竟做错了什么?
@model IEnumerable<DSABankSolution.Models.ExchangeRates>
@{
ViewBag.Title = "Exchange Rates";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<br /> <br />
<input type="text" size="5" value="1" />
@Html.DropDownList("currency", Model.Select(p => new SelectListItem{ Text = p.Name, Value = p.ID}))
to
@Html.DropDownList("currency", Model.Select(p => new SelectListItem { Text = p.Name, Value = p.ID }));
<br /> <br /> <input type="submit" name="Convert" />
汇率模型:
public class ExchangeRates
{
public string ID { get; set; }
public string Name { get; set; }
}
汇率控制器:
public ActionResult Index()
{
IEnumerable<CommonLayer.Currency> currency = CurrencyRepository.Instance.getAllCurrencies().ToList();
//ViewBag.CurrencyID = new SelectList(currency, "ID");
//ViewBag.Currency = new SelectList(currency, "Name");
return View(currency);
}
货币库:
public List<CommonLayer.Currency> getAllCurrencies()
{
var query = from curr
in this.Entity.Currencies
select curr;
return query.ToList();
}
我得到的错误:
传递到字典中的模型项的类型为“System.Collections.Generic.List
1[CommonLayer.Currency]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable
1[DSABankSolution.Models.ExchangeRates]”。
谢谢!