I'm stumped with this one and your help would be most appreicated.
I get the error:
The parameter conversion from type 'System.String' to type 'DataPortal.Models.EntityClasses.FeedbackComment' failed because no type converter can convert between these types
The ModelState.IsValid
is failing on the FeedbackComment.Comment
property
Any ideas?
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; }
}
Controller Methods
//
// 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);
}
And the view
@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>
In my case I was writing a model class name in Html Helper class. which was getting it failed as it was expecting a
System.String
datatype and I was passing a custom class name.The problem is the name of your action parameter:
It's called
comment
. But yourFeedbackComment
also has a property calledComment
of type string. So the default model binder gets crazy. Just rename one of the two to avoid the conflict.For example the following will fix your issue:
I had the same error but the cause was different from the accepted answer. Adding a custom
ModelBinder
fixed it for me.I created a
CData
class as described hereHow do you serialize a string as CDATA using XmlSerializer? (it's the last answer unless it got voted up because it's a great solution)
I serialize my ViewModel to XML. When I set a type in a property in my ViewModel to
CData
, it will automatically be serialized to a CData section and deserialized to aCData
field. (which is great!)When I do a post action in my view I want to keep some fields in my ViewModel so they are added as
Html.HiddenFor
in the form.When a submit is done, an error occurs (the one in the title). The model binder tries to bind a string field to a CData field which fails.
A custom model binder for the
CData
type fixes thisGlobal.asax
I had the same name for the parameter on the GET method
.. and a property from Faturamento model, that was used on the POST method
Just like this..
What solved for me was changing the GET parameter name: