アイテムのコレクションを含むモデルがあります。これらのアイテムは、EditorTemplateを使用して表示されます。モデルにQuantityフィールドの値がない場合、問題はありません。ただし、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>
数量のデータ型はInt64です。辞書が最初のレンダリングではなく、2回目に文字列を必要とする理由がわかりません。
これがコントローラーアクションです。
[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; }
}
}