简体   繁体   English

如果model为null,如何处理null异常,如何在mvc中的视图中处理它?

[英]how to handle null exception if model is null , how to handle it in view in mvc?

In an MVC application in the controller I use Entity Framework and LINQ. 在控制器的MVC应用程序中,我使用实体框架和LINQ。 I'm storing 10 records in a variable then, binding them to the model. 然后,我将10条记录存储在一个变量中,并将它们绑定到模型。 But if there are no records I'm getting error 但是,如果没有记录,我会报错

Index was out of range. 索引超出范围。 Must be non-negative and less than the size of the collection. 必须为非负数并且小于集合的大小。

While I handled null exception in view. 虽然我在视图中处理了null异常。 I'm getting compile time error 我收到编译时间错误

Cannot initialize implicitly typed variable with an array initializer. 无法使用数组初始化程序初始化隐式类型的变量。

View: 视图:

  function initMap() {
            var labels = '12345678910';
            var labelIndex = 0;

           @if (Model != null)
                   {
                       var   myLatLng = { lat:  @Model[0].latitude.ToString(), lng: @Model[0].longitude.ToString() };
                   }

           else
                   {
                          var myLatLng = { lat:0, lng:0};//Bharat Seva Ashram   
                   }

Controller: 控制器:

List<AssetTrackerViewModel> model = new List<AssetTrackerViewModel>();
/// PIR 1 //RAD:DN
try
{
    WebRequest req = WebRequest.Create(@"https://url");
    req.Method = "GET";
    req.Headers["Authorization"] = "Basic " + "pwd==";
    HttpWebResponse resp = req.GetResponse() as HttpWebResponse;
    var encoding = resp.CharacterSet == "" ? Encoding.UTF8 : Encoding.GetEncoding(resp.CharacterSet);
    using (var stream = resp.GetResponseStream())
    {
        var reader = new StreamReader(stream, encoding);
        var responseString = reader.ReadToEnd();

      **//here "items" im getting null / empty** 
       var items = Pirs.Where(a => !a.dataFrame.EndsWith("AAAAAAAAAAA="))
                                         .GroupBy(a => a.dataFrame.Substring(a.dataFrame.Length - 12))
                                         .Select(g => g.First())
                                         .OrderByDescending(a => a.timestamp)
                                         .Take(10);

        foreach (var item in items)
        {
            byte[] data = Convert.FromBase64String(item.dataFrame.ToString());
        }
    }
}

The problem is that the code below is recognized as Razor code while you are probably expecting it to be Javascript code: 问题在于,以下代码可能被识别为Razor代码,而您可能希望它是Javascript代码:

var   myLatLng = { lat:  @Model[0].latitude.ToString(), lng: @Model[0].longitude.ToString() };

Simply add @: before the statements you want to be ignored by Razor: 只需在您想被Razor忽略的语句之前添加@: ::

@if (Model != null)
{
    @: var myLatLng = { lat:  @Model[0].latitude.ToString(), lng: @Model[0].longitude.ToString() };
}
else
{
    @: var myLatLng = { lat:0, lng:0 };
}

只需检查列表是否> 0

@if (Model != null && Model.Count >0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM