Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
240 views
in Technique[技术] by (71.8m points)

ASP.NET MVC3 Model Binding using IEnumerable (Cannot infer type from)

I have a model class (edited for brevity)

Model Class

public class GridModel
{
   public string ItemNumber { get; set; }
   public int OnHandQty     { get; set; }
}
public class Shipment
{
  public string shipTrackingNo               {get; set;}
  public IEnumerable<GridModel> ItemsShipped { get; set;}
{

cshtml page

@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>

I want to be able to bind item.OnHandQty that resides in the IEnumerable collection. How can you have a Model class and also an IEnumerable collection of a custom class (or rather you own class)?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Well, what is the type of the items stored in ItemsShipped? You should use the generic version of IEnumerable to indicate what types are stored within it.

If your class was named Item, you would declare it IEnumerable<Item> then, when iterating at run time, ie @foreach (var Item in Model.ItemsShipped), the type of Item will be strongly-typed instead of a plain object.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...