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>
The problem is the name of your action parameter:
public virtual ActionResult Create(FeedbackComment comment)
It's called comment
. But your FeedbackComment
also has a property called Comment
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:
public virtual ActionResult Create(FeedbackComment model)
{
if (ModelState.IsValid)
{
_feedbackCommentRepository.Add(model);
return RedirectToAction(MVC.Feedback.Details(model.FeedbackId));
}
return View(model);
}
I had the same name for the parameter on the GET method
[HttpGet]
public ActionResult Create(int OSSB)
.. and a property from Faturamento model, that was used on the POST method
[HttpPost]
public ActionResult Create(Faturamento model)
Just like this..
public class Faturamento {
[Column("ID_OSSB")]
public virtual int ID_OSSB { get; set; }
[ForeignKey("ID_OSSB")]
public virtual OSSB OSSB { get; set; }
...
}
What solved for me was changing the GET parameter name:
[HttpGet]
public ActionResult Create(int IDOSSB)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With