为什么我的MVC ViewModel为空?

时间:2015-05-16 19:07:33

标签: asp.net-mvc asp.net-mvc-scaffolding

型号:

public class FullPrice
{
    public IEnumerable<Service> Services { get; set; }
}

视图模型:

@model hotel.Models.FullPrice
@foreach(Service service in Model.Services)
{
    //
}

查看:

#rightPanal {
    position: absolute;
    top: 50%;
}

我的视图模型为NULL。为什么呢?

3 个答案:

答案 0 :(得分:2)

模型将 NULL ,因为正在执行视图时,当前没有实例。

通常,将模型实例传递到视图中的方法将在相应的操作方法中,如

public View myActionMethod()
{
    hotel.Models.FullPrice model = new hotel.Models.FullPrice();
    model.Services = new List<Service>();//Also prevent NULL exception with this property
    return View(model);
}

答案 1 :(得分:1)

我想这不是模型什么是null,而是public class RemoteImageToImageViewAsyncTask extends AsyncTask<Void, Void, Void> { ImageView imageView; String url; File output; public RemoteImageToImageViewAsyncTask(ImageView imageView, String url, File output) { this.imageView = imageView; this.url = url; this.output = output; } @Override protected Void doInBackground(Void... params) { // Downloads the image from url to output ImageDownloader.download(url, output); return null; } @Override protected void onPostExecute(Void params) { Picasso.with(context) .load(output) .into(imageView); } } 。将操作代码更改为

Services

答案 2 :(得分:0)

将ViewModel代码更改为

echo $(ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | cut -d' ' -f1) $(hostname) localhost >> /etc/hosts

所以当你这样做时public class FullPrice { public FullPrice() { this.Services = new List<Service>(); } public IEnumerable<Service> Services { get; set; } } 属性不会为空

Services