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; }
}
}
null
and it is notSystem.Int64
– webdeveloperReleaseModel
, but template usesOrderLineItemModel
, maybe you will see some extra details. – webdeveloper