First Last

First Last

  • 992
  • 648
  • 67.4k

Input field with html tag - how to get it to not show the html

Sep 14 2020 6:47 PM
I have an input field that shows the html tag and also does not do what it is supposed to do. In this case, show the content in 2 lines without the html tag. (like the display only does - shown next.)
  1. <p>reguser1p>My first edited comment.
After "My first edited comment" is entered by the User and before it is saved to the table I prepended it with the 'name of the user' surrounded with the p tag. I did this so when it is subsequently shown either for update via an input tag or display via a div tag that the "My first edited comment" goes to a 2nd line. It works for display but not for edit in an input tag.
 
The data in the database table that is in the model that is used.
  1. <p>reguser1p>My first edited comment.  
I have a display only field that does NOT show the html tag and does what the tag is supposed to do.
 
reguser2
 
My first comment about this new blog site.
 
The data in the database table that is in the model that is used.
  1. <p>reguser2p>My first comment about this new blog site.  
How do I code the input field to display properly - the "My first edited comment" goes to a 2nd line?
 
I'm using what I use for the display only field to decode the HTML - using decode to get rid of the tags.
  1. value="@Html.Raw(WebUtility.HtmlDecode(@blogComment.BlogCommentContent))"  
I tried this as well - but get an error:
  1. <div class="form-control" id="@string.Format("{0}_{1}", "inputEditComment", blogComment.BlogCommentId)" style="display: inline; margin-top: 27px; font-size: 13px; color: #9c9898;">  
  2. @Html.EditorFor(model => WebUtility.HtmlDecode(@blogComment.BlogCommentContent))  
  3. </div>  
The error is:
 
System.InvalidOperationException: 'Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.'
 
Here is the asp.net partial view (not all of it):
  1. @if (blogComment.UserId == blogComment.SignedInUserId)    
  2. {    
  3.     @* Can edit the input field. *@    
  4.     <input type="text" id="@string.Format("{0}_{1}", "inputEditComment", blogComment.BlogCommentId)" class="form-control" value="@Html.Raw(WebUtility.HtmlDecode(@blogComment.BlogCommentContent))" style="display: inline; margin-top: 27px; font-size: 13px; color: #9c9898;" />    
  5.     
  6.     <a href="" class="editComment" data-id="@blogComment.BlogCommentId">Savea>    
  7.     <a href="" class="deleteComment" data-id="@blogComment.BlogCommentId"> Deletea>    
  8. }    
  9. else    
  10. {    
  11.     @* Display only. *@    
  12.     <div style="margin-top: 27px; font-size: 13px; color: #9c9898;">    
  13.         @Html.Raw(WebUtility.HtmlDecode(@blogComment.BlogCommentContent))    
  14.     div>    
  15. }

Answers (5)