ASP.NET MVC null model passed to controller action

Why is the next controller action passed by an empty parameter?

public FileContentResult GetImageForArticle(ArticleSummary article) { if (article == null || !article.ContainsValidThumbNail()) return null; return File(article.ThumbNail, article.ThumbNaiType); } 

from the following partial view:

 <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<AkwiMemorial.Models.ArticleSummary>>" %> <%if (Model.Count() > 0) { %> <table> <% foreach (var item in Model) { %> <tr> <td> <img src='<%=Url.Action("GetImageForArticle", "Resources", new { article = item })%>' alt=""/> </td> </tr> <% } %> </table> 
+4
source share
1 answer

You cannot send complex objects as follows:

 <%=Url.Action("GetImageForArticle", "Resources", new { article = item })%> 

Only simple scalar properties:

 <%=Url.Action("GetImageForArticle", "Resources", new { Id = item.Id, Foo = item.StringFoo, Bar = item.IntegerBar })%> 

Therefore, it is good practice in this case to send only id:

 <%=Url.Action("GetImageForArticle", "Resources", new { id = item.Id }) %> 

and then, so that your controller action selects the appropriate model from any point where it is stored, enter this identifier:

 public ActionResult GetImageForArticle(int id) { ArticleSummary article = _someRepository.GetArticle(id); if (article == null || !article.ContainsValidThumbNail()) { return null; } return File(article.ThumbNail, article.ThumbNaiType); } 
+6
source

Source: https://habr.com/ru/post/1346106/


All Articles