【问题标题】:Crash in runtime in Razor while using ViewModel使用 ViewModel 时 Razor 运行时崩溃
【发布时间】: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


【解决方案1】:

可能太明显了,但看起来你的视图文件缺少这个

@model FreeRolla.Models.ViewModels.HomeView

编辑:

您的视图类应具有以下声明:

namespace FreeRolla.Models.ViewModels
{
    public class HomeView
    {
        public List<LocationBasedPromotion> allLocationBasedPromotions { get; set; }
    }
}

LocationBasedPromotion 类也应设为public。基本上,作为提示,在你的项目中创建每个类public,除非你有充分的理由不这样做。随着您获得更多经验,您会遇到一些情况,您会知道何时不上课public。但在你的情况下,只需将它们设为public

【讨论】:

  • 刚刚在 View 的第一行添加了它。收到以下错误:CS0122:“FreeRolla.Models.ViewModels.HomeView”由于其保护级别而无法访问
  • 将声明改为public class HomeView
  • 做到了。现在我得到:错误 1 ​​不一致的可访问性:属性类型“System.Collections.Generic.List”比属性“FreeRolla.Models.ViewModels.HomeView.allLocationBasedPromotions”更难访问
猜你喜欢
  • 2018-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多