Asp.net mvc 传入字典的模型项的类型为';System.Int64';

Asp.net mvc 传入字典的模型项的类型为';System.Int64';,asp.net-mvc,asp.net-mvc-3,Asp.net Mvc,Asp.net Mvc 3,我有一个包含项目集合的模型,这些项目使用EditorTemplate显示。如果我的模型没有数量字段的值,我就没有问题。但是,如果quantity属性有一个值,则在呈现模板视图时,会在@Html.EditorFor(m=>m.quantity…)上引发以下异常: The model item passed into the dictionary is of type 'System.Int64', but this dictionary requires a model item of type

我有一个包含项目集合的模型,这些项目使用EditorTemplate显示。如果我的模型没有数量字段的值,我就没有问题。但是,如果quantity属性有一个值,则在呈现模板视图时,会在@Html.EditorFor(m=>m.quantity…)上引发以下异常:

The model item passed into the dictionary is of type 'System.Int64', 
but this dictionary requires a model item of type 'System.String'
这是编辑器模板

@model OrderLineItemModel
<tr id="rowid-@Model.ItemID">
    <td class="itemidcell">@Html.HiddenFor(m=>m.ItemID) @Html.DisplayFor(m=>m.ItemID)</td>
    <td>@Html.HiddenFor(m => m.CustomerItemID)@Html.DisplayFor(m=>m.CustomerItemID)</td>
    <td>@Html.HiddenFor(m => m.ItemName)@Html.DisplayFor(m=>m.ItemName)</td>
    <td>@Html.HiddenFor(m => m.BlanketOrderQuantity)@Html.DisplayFor(m=>m.BlanketOrderQuantity)</td>
    <td>@Html.HiddenFor(m => m.ReleasedQuantity)@Html.DisplayFor(m=>m.ReleasedQuantity)</td>
    <td>@Html.HiddenFor(m => m.RemainingQuanity)@Html.DisplayFor(m=>m.RemainingQuanity)</td>
    <td id="cellid-@Model.ItemID">@Html.DisplayFor(m=>m.Price)</td>
    <td class="quantitycell">@Html.EditorFor(m=>m.Quantity, new {@class = "quantitytxt"}) @Html.ValidationMessageFor(m => m.Quantity)</td>
</tr>
using System;
using System.ComponentModel.DataAnnotations;


namespace ViewModels.BlanketOrder {
    public class OrderLineItemModel  {
        [Display(Name = "Customer Item #")]
        public string CustomerItemID { get; set; }

        [Display(Name = "Item #")]
        public string ItemID { get; set; }

        [Display(Name = "Item Name")]
        public string ItemName { get; set; }

        [DisplayFormat(DataFormatString = "#,###")]
        public int? PriceUnit { get; set; }

        public string UnitID { get; set; }


        [Display(Name = "Quantity")]
        [Required(ErrorMessage = "Item Quantity is required")]
        [RegularExpression(@"[0-9]+", ErrorMessage = "Item Quantity must be a whole Number")]
        [Range(1, 15000000, ErrorMessage = "Item Quantity must be between 1 - 15000000")]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:0}", NullDisplayText = "0")]
        public Int64? Quantity { get; set; }

        [Display(Name = "Unit Price")]
        public Decimal? Price { get; set; }

        public Int64 BlanketOrderQuantity { get; set; }
        public Int64 ReleasedQuantity { get; set; }
        public Int64 RemainingQuanity { get; set; }
    }
}
这是用于模板的我的模型

@model OrderLineItemModel
<tr id="rowid-@Model.ItemID">
    <td class="itemidcell">@Html.HiddenFor(m=>m.ItemID) @Html.DisplayFor(m=>m.ItemID)</td>
    <td>@Html.HiddenFor(m => m.CustomerItemID)@Html.DisplayFor(m=>m.CustomerItemID)</td>
    <td>@Html.HiddenFor(m => m.ItemName)@Html.DisplayFor(m=>m.ItemName)</td>
    <td>@Html.HiddenFor(m => m.BlanketOrderQuantity)@Html.DisplayFor(m=>m.BlanketOrderQuantity)</td>
    <td>@Html.HiddenFor(m => m.ReleasedQuantity)@Html.DisplayFor(m=>m.ReleasedQuantity)</td>
    <td>@Html.HiddenFor(m => m.RemainingQuanity)@Html.DisplayFor(m=>m.RemainingQuanity)</td>
    <td id="cellid-@Model.ItemID">@Html.DisplayFor(m=>m.Price)</td>
    <td class="quantitycell">@Html.EditorFor(m=>m.Quantity, new {@class = "quantitytxt"}) @Html.ValidationMessageFor(m => m.Quantity)</td>
</tr>
using System;
using System.ComponentModel.DataAnnotations;


namespace ViewModels.BlanketOrder {
    public class OrderLineItemModel  {
        [Display(Name = "Customer Item #")]
        public string CustomerItemID { get; set; }

        [Display(Name = "Item #")]
        public string ItemID { get; set; }

        [Display(Name = "Item Name")]
        public string ItemName { get; set; }

        [DisplayFormat(DataFormatString = "#,###")]
        public int? PriceUnit { get; set; }

        public string UnitID { get; set; }


        [Display(Name = "Quantity")]
        [Required(ErrorMessage = "Item Quantity is required")]
        [RegularExpression(@"[0-9]+", ErrorMessage = "Item Quantity must be a whole Number")]
        [Range(1, 15000000, ErrorMessage = "Item Quantity must be between 1 - 15000000")]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:0}", NullDisplayText = "0")]
        public Int64? Quantity { get; set; }

        [Display(Name = "Unit Price")]
        public Decimal? Price { get; set; }

        public Int64 BlanketOrderQuantity { get; set; }
        public Int64 ReleasedQuantity { get; set; }
        public Int64 RemainingQuanity { get; set; }
    }
}

看看我对自己问题的回答

你也许能做到这一点


@Html.ValidationMessageFor(m=>(string)m.Quantity.ToString()??0)

看看我对自己问题的回答

你也许能做到这一点


@Html.ValidationMessageFor(m=>(string)m.Quantity.ToString()??0)

您的控制器操作是什么样子的,我的意思是错误被处理了吗?我认为用户发送的空字符串变成了
null
,而不是
系统。Int64
属性,Quantity是Int64的一个可为null的值。这个值在最初加载时为null,然后就可以工作了。您可能需要创建自己的编辑器模板te以支持nullable long。请参阅编辑器模板部分您需要设置断点并查看您的模型。Controller接收
ReleaseModel
,但模板使用
OrderLineItemModel
,您可能会看到一些额外的详细信息。您的控制器操作看起来如何,我的意思是错误是否得到了处理?我认为用户发送了空字符串,它变成了
null
,而不是
System.Int64
属性,Quantity是Int64的一个可空值。初始加载时该值为null,但随后仍有效。您可能必须创建自己的编辑器模板以支持可空长。请参阅编辑器模板部分您需要设置断点并查看您的模型。控制器receive
ReleaseModel
,但是模板使用了
OrderLineItemModel
,也许您会看到一些额外的详细信息。验证消息不是问题所在。我可以完全删除它,但仍然会遇到问题。问题是EditorFor尝试使用字符串编辑器模板,并且属性的数据类型为Long(Int64)验证消息不是问题所在。我可以完全删除它,但仍然会遇到问题。问题在于EditorFor正在尝试使用字符串编辑器模板,并且属性的数据类型为Long(Int64)