【问题标题】:The model item passed into the dictionary is of type 'System.Threading.Tasks.Task`1[System.Collections.Generic.IEnumerable`传递到字典中的模型项的类型为 'System.Threading.Tasks.Task`1[System.Collections.Generic.IEnumerable`
【发布时间】:2015-12-19 07:26:20
【问题描述】:

运行我的 mvc 应用程序时出现以下错误:

{"The model item passed into the dictionary is of type 
'System.Threading.Tasks.Task`1[System.Collections.Generic.IEnumerable`
1[PM.CManager.Clm.Domain.Models.Ln]]', b
ut this dictionary requires a model item of type 'PM.CManager.Clm.Domain.Models.Ln'."}

下面是我的控制器返回值:

 public ActionResult ClaimDetail()
        {
            //return View();
            string id = "1000000246";
             if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            var LnDetail = _LnProxy.GetLnDetailByLnNum((string)id);
            if (LnDetail == null)
            {
                return HttpNotFound();
            }
            return View(LnDetail);
        }

以下是我的看法:

@model PM.CManager.Clm.Domain.Models.Ln
   @using (Html.BeginForm())
   {
       <div class="panel panel-primary">

           <div class="panel-heading inform" style="">
               <table clases="panel-title inform">
                   <tr>
                       <td class="inform">Ln Number: <label id="Lnnum" name="Lnnum">1000100001</label></td>
                       <td class="inform">Status: <label id="Lnstatus" name="Lnstatus">Forclosure</label></td>
                       <td class="inform">Ln Type: <label id="Lntype" name="Lntype">Government(FHA)</label></td>
                   </tr>
               </table>
           </div>

为了解决这个问题,需要对下面的行做哪些更改?

@model PM.CManager.Clm.Domain.Models.Ln

【问题讨论】:

  • _LnProxy.GetLnDetailByLnNum((string)id) 返回什么?您需要在您的视图中完全匹配。
  • 首先,您需要等待 _LnProxy.GetLnDetailByLnNum((string)id) 返回的内容,因为它是一个任务。其次,您的视图需要单个项目,但该函数返回一个集合。在这种情况下,我们无能为力。

标签: c# .net model-view-controller task-parallel-library


【解决方案1】:

您似乎正在从这一行返回一个任务:

var LnDetail = _LnProxy.GetLnDetailByLnNum((string)id);

这需要改成:

var LnDetail = await _LnProxy.GetLnDetailByLnNum((string)id);

那你需要把方法的签名改成:

public async Task<ActionResult> ClaimDetail() { /* ... */ }

还请记住,如果它是一个集合或单个项目,并相应地调整您的视图。

【讨论】:

  • 谢谢,它有效,但现在我收到此错误,传递到字典中的模型项的类型为 'System.Collections.Generic.List`1[PM.CManager.Cm.Domain.Models .Ln]',但是这个字典需要一个'PM.CManager.Cm.Domain.Models.Ln'类型的模型项。
  • 是的,这是因为它是您要返回的收藏。将您的视图更改为@model IList&lt;PM.CManager.Clm.Domain.Models.Ln&gt;
猜你喜欢
  • 2012-04-24
  • 2015-02-07
  • 1970-01-01
  • 1970-01-01
  • 2013-10-17
  • 1970-01-01
  • 2012-06-25
  • 2022-01-16
  • 2015-09-19
相关资源
最近更新 更多