2011-11-02 24 views
27

Tôi bị gộp với lệnh này và trợ giúp của bạn sẽ được ứng dụng nhiều nhất.Chuyển đổi tham số từ loại 'System.String' thành loại '' X 'không thành công vì không có trình chuyển đổi loại nào có thể chuyển đổi giữa các loại này

tôi nhận được lỗi:

The parameter conversion from type 'System.String' to type 'DataPortal.Models.EntityClasses.FeedbackComment' failed because no type converter can convert between these types

Các ModelState.IsValid là không trên FeedbackComment.Comment tài sản

Bất kỳ ý tưởng?

public class FeedbackComment : IFeedbackComment 
{ 
    [Key] 
    public int Id { get; set;} 

    public int FeedbackId { get; set; } 

    [Required(ErrorMessage = "Please enter a Comment")] 
    public string Comment { get; set; } 

    public DateTime CommentDate { get; set; } 

    public string CommentBy { get; set; } 
} 

điều khiển phương pháp

// 
    // GET: /FeedbackComment/Create 

    public virtual ActionResult Create(int feedbackId) 
    { 
     var comment = new FeedbackComment {FeedbackId = feedbackId, CommentBy = User.Identity.Name, CommentDate = DateTime.Now}; 
     return View(comment); 
    } 

    // 
    // POST: /FeedbackComment/Create 

    [HttpPost] 
    public virtual ActionResult Create(FeedbackComment comment) 
    { 

     if (ModelState.IsValid) 
     { 

      _feedbackCommentRepository.Add(comment); 

      return RedirectToAction(MVC.Feedback.Details(comment.FeedbackId)); 
     } 

     return View(comment); 
    } 

Và quan điểm

@model DataPortal.Models.EntityClasses.FeedbackComment 
@{ 
ViewBag.Title = "Create Comment"; 
} 
<h2>Create Comment</h2> 
@using (Html.BeginForm()) { 
@Html.ValidationSummary(true) 
<fieldset> 
    <legend>Feedback Comment</legend> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.Comment) 
    </div> 
    <div class="editor-field"> 
     @Html.TextAreaFor(model => model.Comment, new{@class = "TextEditor"}) 
     @Html.ValidationMessageFor(model => model.Comment) 
    </div> 


    @Html.HiddenFor(model=> model.CommentDate) 
    @Html.HiddenFor(model=> model.CommentBy)  
    @Html.HiddenFor(model=> model.FeedbackId) 

    <p> 
     <input type="submit" value="Create" /> 
    </p> 
</fieldset> 
} 

<div> 
@Html.ActionLink("Back to Comment Details", MVC.Feedback.Details(Model.FeedbackId)) 
</div> 
+2

Thử thay đổi tên thông số nhận xét phản hồi trong hành động POST của bạn. Ngoài ra, bạn nên đặt CommentBy và CommentDate trong hành động POST của mình thay vì sử dụng trường bị ẩn – epzee

+0

Cảm ơn bạn rất nhiều vì mẹo phụ của bạn. – MrBliz

Trả lời

62

Vấn đề là tên của tham số hành động của bạn:

public virtual ActionResult Create(FeedbackComment comment) 

Nó được gọi là comment. Nhưng FeedbackComment của bạn cũng có một thuộc tính được gọi là Comment của chuỗi loại. Vì vậy, chất kết dính mô hình mặc định trở nên điên rồ. Chỉ cần đổi tên một trong hai để tránh xung đột.

Ví dụ sau đây sẽ khắc phục vấn đề của bạn:

public virtual ActionResult Create(FeedbackComment model) 
{ 
    if (ModelState.IsValid) 
    { 
     _feedbackCommentRepository.Add(model); 
     return RedirectToAction(MVC.Feedback.Details(model.FeedbackId)); 
    } 
    return View(model); 
} 
+1

Ah, tất nhiên, có ý nghĩa hoàn toàn khi bạn nghĩ về nó. Luôn luôn nghĩ rằng bạn sẽ được an toàn từ những loại shenanigans như C# là trường hợp nhạy cảm. Cảm ơn nhiều. – MrBliz

+0

Lượng thời gian đáng kinh ngạc bị lãng phí với lỗi này. 1 cho bạn và cảm ơn! – Matty

+0

Địa ngục đẫm máu !!! Trường hợp của nó thậm chí không nhạy cảm !!!! Cuối cùng vấn đề đã được sửa chữa – Rusty

0

tôi đã có lỗi tương tự nhưng nguyên nhân là khác nhau từ các câu trả lời được chấp nhận. Thêm tùy chỉnh ModelBinder đã sửa nó cho tôi.

Tôi tạo ra một lớp CData như mô tả ở đây

How do you serialize a string as CDATA using XmlSerializer? (đó là câu trả lời cuối cùng trừ khi nó đã bình chọn bởi vì đó là một giải pháp tuyệt vời)

tôi serialize ViewModel của tôi để XML. Khi tôi đặt loại trong một thuộc tính trong ViewModel của tôi thành CData, nó sẽ tự động được tuần tự hóa thành một phần CData và được tuần tự hóa thành trường CData. (tuyệt vời!)

Khi tôi thực hiện hành động đăng trong quan điểm của mình, tôi muốn giữ một số trường trong ViewModel của mình để chúng được thêm vào dưới dạng Html.HiddenFor trong biểu mẫu.

Khi gửi xong, lỗi xảy ra (một trong tiêu đề). Trình liên kết mô hình cố gắng liên kết một trường chuỗi với trường CData không thành công.

Một mô hình tùy chỉnh chất kết dính cho các bản sửa lỗi CData loại này

public class CDataModelBinder : IModelBinder 
{ 
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) 
    { 
     var result = bindingContext.ValueProvider.GetValue(bindingContext.ModelName); 
     return new CData(result.AttemptedValue); 
    } 
} 

Global.asax

ModelBinders.Binders.Add(typeof(CData), new CDataModelBinder()); 
1

tôi đã có tên tương tự cho các tham số vào phương pháp GET

[HttpGet] 
public ActionResult Create(int OSSB) 

..và một tài sản từ mô hình Faturamento, đã được sử dụng trên các phương thức POST

[HttpPost] 
public ActionResult Create(Faturamento model) 

Chỉ cần như thế này ..

public class Faturamento { 
     [Column("ID_OSSB")] 
     public virtual int ID_OSSB { get; set; } 
     [ForeignKey("ID_OSSB")] 
     public virtual OSSB OSSB { get; set; } 
     ... 
} 

gì giải quyết đối với tôi là thay đổi tên tham số GET:

[HttpGet] 
public ActionResult Create(int IDOSSB) 
Các vấn đề liên quan