2

我有一个包含项目集合的模型,这些项目使用 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; }
    }
}
4

1 回答 1

0

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

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

你也许可以做到这一点。

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

于 2012-09-19T12:40:51.567 回答