问题描述
我正在尝试将值显示在droplist中,但是它会出现转换错误。
我的viewmodel是
I am trying to display values into droplist but it is getting casting error.
my viewmodel is
public class TaxiLogVM : BaseVM
{
<pre lang="cs">public int UserId { get; set; }
public int VenueId { get; set; }
Public IList<TaxiFirmVM> TaxiFirmVM { get; set; }
}
出租车frim vm模型
Taxi frim vm model
public class TaxiFirmVM:BaseVM
{
public string Name { get; set; }
}
和业务逻辑是
And business logic is
string sql = "select Name from [TaxiFirm] Where VenueId = {0}";
var taxifirmVM= dc.Database.SqlQuery<TaxiFirmVM>(sql, vm.VenueId).FirstOrDefault();
return new TaxiLogVM() { VenueId = vm.VenueId, Title = new Venue().GetUserAndVenueName(vm, dc), TaxiFirmVM = taxifirmVM};
我的html是
And my html is
@Html.DropDownListFor(model => model.VenueId, Model.TaxiFirmVM);
但是iam得到以下错误
错误7无法将类型'SGSModel.TaxiFirmVM'隐式转换为'System.Collections.Generic.IList< sgsmodel.taxifirmvm>'。存在显式转换(你是否错过演员表?)
如何解决这个问题。?
请有人帮助我..
谢谢
But iam Getting Following Error
Error7Cannot implicitly convert type 'SGSModel.TaxiFirmVM' to 'System.Collections.Generic.IList<sgsmodel.taxifirmvm>'. An explicit conversion exists (are you missing a cast?)
How to solve this problem.?
Please anyone Help me..
Thanks
推荐答案
var taxifirmVM= dc.Database.SqlQuery<taxifirmvm>(sql, vm.VenueId).FirstOrDefault();</taxifirmvm>
因为FirstOrDefault返回TaxiFirmVM类对象。但是下拉列表期待IList< taxifirmvm>。因此,您需要更改以下代码
because FirstOrDefault return TaxiFirmVM class object. But the dropdownlistfor expect IList<taxifirmvm>. So you need to change the code like below
var taxifirmVM= dc.Database.SqlQuery<taxifirmvm>(sql, vm.VenueId).ToList();</taxifirmvm>
希望这有助于
Hope this helps
这篇关于如何使用视图模型中的HTML Helper显示下拉列表。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!