简体   繁体   English

ASP.NET MVC ViewData Null

[英]ASP.NET MVC ViewData Null

I am currently creating an e-commerce site using C# ASP.NET MVC and have just come across a problem. 我目前正在使用C#ASP.NET MVC创建一个电子商务网站,并且遇到了一个问题。 On pages such as product pages and search results pages, I have to pass Lists of data from my controller to the ViewPage and that works just fine. 在产品页面和搜索结果页面等页面上,我必须将数据列表从我的控制器传递到ViewPage,并且工作正常。 However, a null reference exception occurs if the Viewdata equals null. 但是,如果Viewdata等于null,则会发生空引用异常。 It happens inside the viewpage when it loops through the ViewData and displays products or reviews. 当它遍历ViewData并显示产品或评论时,它会在视图内部发生。

//ProductController.cs //ProductController.cs

public ActionResult Products_Sub(string category, int page)
{
ViewData["Products"] = database.GetByCategory(category, page);
return View();
}

//ViewPage.cs -- product loop //ViewPage.cs - 产品循环

 <ul> foreach (E_Store.Models.Product product in ViewData["Products"] as  
 List<e_store.models.product>)
 {%> 
 <li>
 <img alt="<%= product.Title%>" src="<%= product.Thumbnail %>" /> 
 <a href="/<%=product.Category %>/<%= product.SubCategory %>/<%= product.ASIN %>/1">
 <%=product.Title%></a>
 </li>
 }%>
 </ul>    

The Null Reference Exception occurs when the following piece of code is reached: 到达以下代码时会出现Null Reference Exception:

  <ul> foreach (E_Store.Models.Product product in ViewData["Products"] as

What I would like to know is the best way to catch this type of error if it does happen, without resorting to if statements that check to see if it is null. 我想知道的是,如果确实发生了这种错误,最好的方法是捕获这种类型的错误,而不是求助于检查它是否为null的if语句。

If anyone knows of a good way of doing this I would really love to know. 如果有人知道这样做的好方法,我真的很想知道。

In your case I would have your database.GetByCategory(category, page) method simply return an empty list rather than null, this way your foreach statement just won't have any data to loop through but you won't get a null exception. 在你的情况下,我会有你的database.GetByCategory(category,page)方法只返回一个空列表而不是null,这样你的foreach语句就没有任何数据可以循环但你不会得到一个null异常。

In the case that you're not dealing with a list and the item is null, we use 如果您没有处理列表并且该项为null,我们将使用

Html.Encode(ViewData.Eval("Field"))

To get the item's HTML value, it will return "" if the item is null. 要获取项目的HTML值,如果项目为空,它将返回“”。

you can always use the null coalescing operator inline like this: 你总是可以像这样内联使用null合并运算符:

<%= product.Title ?? "" %>

..but you will just end up with tags with empty attributes. ..但是你最终会得到带有空属性的标签。 prob not what you want. 不要问你想要什么。

you will prb need to use ifs somewhere. 你会prb需要在某个地方使用ifs。 the problem calls for it. 问题需要它。 but the question is "where can you put them without making it messy?" 但问题是“你可以把它们放在哪里而不会让它变得混乱?”

i'm ok wo wrap the whole thing in an if. 我很好,把整个东西包裹在一个if中。 if you're not maybe try a Html extension method for each item of your loop. 如果你不是,可以为循环的每个项目尝试一个Html扩展方法。 pass in the ViewData model object and maybe a bool condition to test for eval. 传递ViewData模型对象,也许是一个bool条件来测试eval。 i overload the Html.Encode() to Html.IfEncode(condition, stringIfTrue, stringIfFalse) you would try something similar, but for the whole image and anchor group. 我将Html.Encode()重载到Html.IfEncode(condition,stringIfTrue,stringIfFalse)你会尝试类似的东西,但对于整个图像和锚组。 else maybe build the group in the code behind for that view and just use a 否则可能在该视图后面的代码中构建组,只需使用

<% =GetImageAndLinkHtml() %>

..for example. ..例如。

let me know if you need more info 如果您需要更多信息,请告诉我

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

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