Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 3 使用IEnumerable的ASP.NET MVC3模型绑定(无法从中推断类型)_Asp.net Mvc 3_Model - Fatal编程技术网

Asp.net mvc 3 使用IEnumerable的ASP.NET MVC3模型绑定(无法从中推断类型)

Asp.net mvc 3 使用IEnumerable的ASP.NET MVC3模型绑定(无法从中推断类型),asp.net-mvc-3,model,Asp.net Mvc 3,Model,为了简洁起见,我编辑了一个模型类 模范班 cshtml页面 我希望能够绑定驻留在IEnumerable集合中的item.OnHandQty。您如何拥有一个模型类和一个自定义类的IEnumerable集合,或者更确切地说,您拥有自己的类 那么,ItemsShipped中存储的项目类型是什么?您应该使用IEnumerable的通用版本来指示其中存储的类型 如果您的类被命名为Item,则在运行时迭代时,即Model.ItemsShipped中的@foreach var Item时,您会将其声明为IEn

为了简洁起见,我编辑了一个模型类

模范班

cshtml页面


我希望能够绑定驻留在IEnumerable集合中的item.OnHandQty。您如何拥有一个模型类和一个自定义类的IEnumerable集合,或者更确切地说,您拥有自己的类

那么,ItemsShipped中存储的项目类型是什么?您应该使用IEnumerable的通用版本来指示其中存储的类型

如果您的类被命名为Item,则在运行时迭代时,即Model.ItemsShipped中的@foreach var Item时,您会将其声明为IEnumerable,项的类型将是强类型而不是普通对象。

而不是此:

@foreach (var Item in Model.ItemsShipped)
           {
             <td width="70px" align="center">
                   @html.LabelFor(item.OnHandQty) <-- Cannot infer type from usage
             </td>
           }
然后在Views/Shared/DisplayTemplates/GridModel.cshtml中创建一个自定义显示模板:

我感觉它不起作用,因为您没有将表达式传递给LabelFor方法

上面的方法比显式for循环更好、更健壮

@model Namespc.Models.Shipment

<link href="../../Content/CSS/Grid/Grid.css" rel="stylesheet" type="text/css" />
<script src="../../Scripts/ECommerce.Grid.js" type="text/javascript"></script>

<div id="_shipmentDetailGrid">
    <table class="TableStyle">
     <tr class="GridRowStyle">
       <td width="130px" ></td>
         @foreach (var Item in Model.ItemsShipped)
           {
             <td width="70px" align="center">
                   @html.LabelFor(item.OnHandQty) <-- Cannot infer type from usage
             </td>
           }
       </tr>
@foreach (var Item in Model.ItemsShipped)
           {
             <td width="70px" align="center">
                   @html.LabelFor(item.OnHandQty) <-- Cannot infer type from usage
             </td>
           }
@Html.DisplayFor(model => model.ItemsShipped)
@model Namespc.Models.GridModel
<td width="70px" align="center">
   @html.LabelFor(model => model.OnHandQty)
</td>