2
votes

I've got a model that contains a collection of items, these items are displayed using an EditorTemplate. If my model does not have a value for the Quantity field, I have no problems. However, if the quantity property has a value, when the template view is rendered the following exception is thrown on the @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'

Here is the editor template.

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

The line that is failing is this one.

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

Quantity has a data type of Int64. I'm not sure why the dictionary requires a String the second time and not on the initial rendering.

Here is the Controller Action.

    [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);

    }

Here is my Model that is being used for the template.

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; }
    }
}
1
how does your controller action look like, I mean were the error was handled?codingbiz
I think user send empty string, which become null and it is not System.Int64webdeveloper
The property, Quantity is a nullable of Int64. This value is null when initially loaded, and yet it works then.Jeff Reddy
You might have to create your own editor template to support nullable long. See bradwilson.typepad.com/blog/2009/10/… Editor Templates sectionLuke Hutton
You need to set breakpoint and view your model. Controller receive ReleaseModel, but template uses OrderLineItemModel, maybe you will see some extra details.webdeveloper

1 Answers

0
votes

Look at my answer to my own question.

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

You might be able to do this.

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