c# - The parameter conversion from type 'System.String' to type ''X' failed because no type converter can convert between these types -


i'm stumped 1 , appreicated.

i error:

the parameter conversion type 'system.string' type 'dataportal.models.entityclasses.feedbackcomment' failed because no type converter can convert between these types

the modelstate.isvalid failing on feedbackcomment.comment property

any ideas?

public class feedbackcomment : ifeedbackcomment {     [key]     public int id { get; set;}      public int feedbackid { get; set; }       [required(errormessage = "please enter 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 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 comment details", mvc.feedback.details(model.feedbackid)) </div> 

the problem name of action parameter:

public virtual actionresult create(feedbackcomment comment) 

it's called comment. feedbackcomment has property called comment of type string. default model binder gets crazy. rename 1 of 2 avoid conflict.

for example following fix issue:

public virtual actionresult create(feedbackcomment model) {     if (modelstate.isvalid)     {         _feedbackcommentrepository.add(model);         return redirecttoaction(mvc.feedback.details(model.feedbackid));     }     return view(model); } 

Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -