【发布时间】:2015-06-15 20:06:51
【问题描述】:
我正在尝试通过 ViewModel 显示多个列表。目前从一个开始,但我收到以下运行时错误:
System.Core.dll 中出现“Microsoft.CSharp.RuntimeBinder.RuntimeBinderException”类型的异常,但未在用户代码中处理
附加信息:“object”不包含“allLocationBasedPromotions”的定义
using System.Text;
using System.Threading.Tasks;
namespace FreeRolla.BaseObjects
{
class LocationBasedPromotion
{
public string Country { get; set; }
public string City { get; set; }
public string FlagIcon { get; set; }
public int HotelCount { get; set; }
public int StartingPrice { get; set; }
public string WeatherIcon { get; set; }
public string WeatherTemperature { get; set; }
public string CityImage { get; set; }
public List<LocationBasedPromotion> Promotions { get; set; }
}
}
using FreeRolla.BaseObjects;
namespace FreeRolla.BL
{
class HPLocationBasedPromotionProvider
{
public List<LocationBasedPromotion> GetAllPromotions()
{
return new List<LocationBasedPromotion>{
new LocationBasedPromotion{Country="UK", City="London", FlagIcon="", HotelCount=135, StartingPrice=350, CityImage="London.jpg", WeatherIcon="cloudy", WeatherTemperature="+18" },
new LocationBasedPromotion{Country="Spain", City="Barcelona", FlagIcon="", HotelCount=215, StartingPrice=230, CityImage="Barcelona.jpg", WeatherIcon="sunny", WeatherTemperature="+28" },
new LocationBasedPromotion{Country="Israel", City="Tel-Aviv", FlagIcon="", HotelCount=75, StartingPrice=280, CityImage="Tel-Aviv.jpg", WeatherIcon="sunny", WeatherTemperature="+32" }
};
}
}
}
using FreeRolla.BaseObjects;
using System.Web;
using System.Web.Mvc;
namespace FreeRolla.Models.ViewModels
{
class HomeView
{
public List<LocationBasedPromotion> allLocationBasedPromotions { get; set; }
}
}
using FreeRolla.BL;
using FreeRolla.Models.ViewModels;
namespace FreeRolla.Controllers
{
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
//return View();
HPLocationBasedPromotionProvider _BigPromotions = new HPLocationBasedPromotionProvider();
HomeView hv = new HomeView();
hv.allLocationBasedPromotions = _BigPromotions.GetAllPromotions();
return View(hv);
}
}
}
从视图 - 这里发生崩溃:
@foreach (var item in Model.allLocationBasedPromotions)
{
【问题讨论】:
-
你能显示视图吗?您是否能够在调试时捕获异常?如果有,具体发生在哪里?
-
看起来您要么输入了错误的视图,要么传递了错误的模型,或者根本没有。
-
基于该错误,您似乎遇到了一些参考问题,但我认为您的
HomeView类与您的HomeController类位于同一项目/命名空间中。 -
感谢您的帮助。我已经用命名空间和发生崩溃的剃刀更新了问题。
标签: c# asp.net-mvc razor