【问题标题】:The model item passed into the dictionary is of type 'System.Int64'传入字典的模型项的类型为“System.Int64”
【发布时间】:2012-09-18 21:00:09
【问题描述】:

我有一个模型,其中包含一组项目,这些项目使用 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>

失败的那一行就是这一行。

<td class="quantitycell">@Html.EditorFor(m=>m.Quantity, new {@class = "quantitytxt"}) @Html.ValidationMessageFor(m => m.Quantity)</td>

Quantity 的数据类型为 Int64。我不确定为什么字典第二次需要 String 而不是初始渲染。

这是控制器动作。

    [HttpPost]
    public ActionResult Release(ReleaseModel model) {          

        var errors = _orderProcessorService.ValidateOrder(model);

        if (errors.Count > 0) {
            foreach (var orderValidationError in errors) {
                ModelState.AddModelError(orderValidationError.Name, orderValidationError.Description);
            }
        }

        if (! ModelState.IsValid) {

            return View(model);
        }


        var response = _orderProcessorService.SubmitOrder(model);

        var responseModel = new OrderResponseModel();
        if (response.OrderStatus == Enumerations.OrderStatus.Success) {
            responseModel.Message = "Thank you for submitting your order. Your sales representative will contact you with any questions concerning your order.";
            responseModel.OrderStatus = "successful";
        }
        else {
            responseModel.Message = "We are sorry, but something has happened during your order submission and your order wasn't processed successfully. Please contact your sales representative regarding this order submission.";
            responseModel.OrderStatus = "failed";
        }


        return View("OrderSubmitted", responseModel);

    }

这是我用于模板的模型。

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; }
    }
}

【问题讨论】:

  • 您的控制器操作如何,我的意思是错误是否已处理?
  • 我认为用户发送了空字符串,它变成了null,而不是System.Int64
  • 属性 Quantity 是 Int64 的可空值。此值在最初加载时为 null,但随后仍有效。
  • 您可能必须创建自己的编辑器模板以支持可空长。请参阅bradwilson.typepad.com/blog/2009/10/… 编辑器模板部分
  • 您需要设置断点并查看您的模型。控制器接收ReleaseModel,但模板使用OrderLineItemModel,也许你会看到一些额外的细节。

标签: asp.net-mvc asp.net-mvc-3


【解决方案1】:

看看我对自己问题的回答。

https://stackoverflow.com/questions/10723782/how-can-i-pass-an-int-value-as-the-linktext-for-and-actionlink/10726097#10726097

你也许可以这样做。

@Html.ValidationMessageFor(m =&gt; (string)m.Quantity.ToString() ?? 0)

【讨论】:

  • 验证消息不是问题。我可以完全删除它,但我仍然遇到问题。问题是 EditorFor 正在尝试使用字符串编辑器模板,并且属性的数据类型为 Long (Int64)
猜你喜欢
  • 2019-06-01
  • 2021-10-01
  • 2011-02-14
  • 2021-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多