ASP.NET MVC空模型传递到控制器动作 [英] ASP.NET MVC null model passed to controller action

查看:130
本文介绍了ASP.NET MVC空模型传递到控制器动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么被传递到下面的控制器动作null参数?

 公共FileContentResult GetImageForArticle(ArticleSummary文章)
        {
            如果(文章== NULL || article.ContainsValidThumbNail()!)返回NULL;
            返回文件(article.ThumbNail,article.ThumbNaiType);
        }

从以下局部视图:

 &LT;%@控制语言=C# Inherits=\"System.Web.Mvc.ViewUserControl<IEnumerable<AkwiMemorial.Models.ArticleSummary>>\" %GT;
&下;如果%(Model.Count()大于0)
  {%GT;
&LT;表&gt;
    &LT;%的foreach(在型号VAR项)
       {%GT;
        &所述; TR&GT;
             &所述; TD&GT;
               &LT; IMG SRC ='&LT;%= Url.Action(GetImageForArticle,资源,新的{文章=项目})%GT;' ALT =/&GT;
            &LT; / TD&GT;
        &LT; / TR&GT;    &LT;%}%GT;    &LT; /表&gt;


解决方案

您不能发送复杂的对象是这样的:

 &LT;%= Url.Action(GetImageForArticle,资源,新的{文章=项目})%GT;

只有简单的标量的属性:

 &LT;%= Url.Action(GetImageForArticle,资源,新{
    ID = item.Id,
    美孚= item.StringFoo,
    酒吧= item.IntegerBar
})%GT;

因此​​,在这种情况下,一个很好的做法是只发送ID:

 &LT;%= Url.Action(GetImageForArticle,资源,新{ID = item.Id})%GT;

再有你的控制器动作取不管它是存储给这个ID对应型号:

 公众的ActionResult GetImageForArticle(INT ID)
{
    ArticleSummary文章= _someRepository.GetArticle(ID);
    如果(文章== NULL ||!article.ContainsValidThumbNail())
    {
        返回null;
    }
    返回文件(article.ThumbNail,article.ThumbNaiType);
}

Why is a null parameter being passed to the following controller action?

 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>

解决方案

You cannot send complex objects like this:

<%=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
})%>

So a good practice in this case is to only send an id:

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

and then have your controller action fetch the corresponding model from wherever it is stored give this id:

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

这篇关于ASP.NET MVC空模型传递到控制器动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆