2012-09-18 3 views
2

컬렉션이 포함 된 모델이 있는데이 항목은 EditorTemplate을 사용하여 표시됩니다. 모델에 수량 필드의 값이없는 경우 아무런 문제가 없습니다. 수량 속성 값을 갖는 경우에는, 템플릿 뷰 렌더링 다음 예외에 발생되는 @ Html.EditorFor (m => m.Quantity ... : 여기 사전에 전달 된 모델 항목의 형식은 'System.Int64'입니다.

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="[email protected]"> 
    <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="[email protected]">@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의 데이터 유형이 있습니다. 나는에 사전이 문자열을 두 번째 시간을 필요로 이유를 모르겠어요하지 초기 운명 반지.

다음은 컨트롤러 동작입니다.

[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; } 
    } 
} 
+0

컨트롤러 동작이 어떻게 보이나요? 오류가 처리 되었습니까? – codingbiz

+1

사용자가 'null'이되는 빈 문자열을 보내고 System.Int64가 아니라고 생각합니다. – webdeveloper

+0

속성은 Int64의 nullable 속성입니다. 이 값은 처음로드 될 때 null이며 아직 작동합니다. –

답변

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)

+0

유효성 검사 메시지가 문제가 아닙니다. 나는 그것을 완전히 제거 할 수 있으며 여전히 문제가 있습니다. 문제는 EditorFor가 문자열 편집기 템플릿을 사용하려고하며 속성이 Long (Int64) 데이터 유형의 속성입니다. –

관련 문제