本文介绍了从类型'System.String'的输入'X'的参数转换失败,因为没有类型转换器可在这些类型之间的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我难住了这一个和你的帮助将是非常美联社preicated。

我得到的错误

 从类型'System.String'的参数转换成键入'DataPortal.Models.EntityClasses.FeedbackComment失败,因为没有类型转换器可在这些类型之间的转换
 

该ModelState.IsValid失败的FeedbackComment.Comment财产

任何想法?

 公共类FeedbackComment:IFeedbackComment
{
    [键]
    公众诠释编号{获得;组;}

    公众诠释FeedbackId {获得;组; }

     [必需的(的ErrorMessage =请输入评论)
    公共字符串评论{获得;组; }

    公开日期时间CommentDate {获得;组; }

    公共字符串CommentBy {获得;组; }
}
 

控制器方法

  //
    // GET:/ FeedbackComment /创建

    公共虚拟的ActionResult创建(INT feedbackId)
    {
        VAR评论=新FeedbackComment {FeedbackId = feedbackId,CommentBy = User.Identity.Name,CommentDate = DateTime.Now};
        返回查看(评论);
    }

    //
    //邮编:/ FeedbackComment /创建

    [HttpPost]
    公共虚拟的ActionResult创建(FeedbackComment评论)
    {

        如果(ModelState.IsValid)
        {

            _feedbackCommentRepository.Add(注解);

            返回RedirectToAction(MVC.Feedback.Details(comment.FeedbackId));
        }

        返回查看(评论);
    }
 

和视图

  @model DataPortal.Models.EntityClasses.FeedbackComment
@ {
ViewBag.Title =创建注释;
}
< H2>创建注释和LT; / H>
 
  @using(Html.BeginForm()){
@ Html.ValidationSummary(真)
<字段集>
    <传奇>评论内容:LT; /传说>

    < D​​IV CLASS =编辑标记>
        @ Html.LabelFor(型号=> model.Comment)
    < / DIV>
    < D​​IV CLASS =主编场>
        @ Html.TextAreaFor(型号=> model.Comment,新{@class =文本编辑})
        @ Html.ValidationMessageFor(型号=> model.Comment)
    < / DIV>


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

    &其中p为H.;
        <输入类型=提交值=创建/>
    &所述; / P>
< /字段集>
}

< D​​IV>
@ Html.ActionLink(回到意见详细资料,MVC.Feedback.Details(Model.FeedbackId))
< / DIV>
 

解决方案

现在的问题是您的操作参数的名称:

 公共虚拟的ActionResult创建(FeedbackComment评论)
 

这就是所谓的评论。但是,你的 FeedbackComment 也有string类型的称为财产注释。所以默认模型绑定变得疯狂。只需重命名这两个中的一个,以避免冲突。

例如下面将解决您的问题:

 公共虚拟的ActionResult创建(FeedbackComment模型)
{
    如果(ModelState.IsValid)
    {
        _feedbackCommentRepository.Add(模型);
        返回RedirectToAction(MVC.Feedback.Details(model.FeedbackId));
    }
    返回查看(模型);
}
 

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);
}

这篇关于从类型'System.String'的输入'X'的参数转换失败,因为没有类型转换器可在这些类型之间的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 23:23