我被這個難住了,你的幫助將是最適合的。從類型'System.String'到類型''X'的參數轉換失敗,因爲沒有類型轉換器可以在這些類型之間進行轉換
我得到的錯誤:
The parameter conversion from type 'System.String' to type 'DataPortal.Models.EntityClasses.FeedbackComment' failed because no type converter can convert between these types
的ModelState.IsValid
失敗的FeedbackComment.Comment
財產
任何想法?
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; }
}
控制器方法
//
// 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);
}
和視圖
@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>
嘗試更改POST操作中的FeedbackComment參數名稱。另外,您應該在POST操作中設置CommentBy和CommentDate,而不是使用隱藏字段 – epzee
非常感謝您的輔助提示。 – MrBliz