Tangara G

Tangara G

  • NA
  • 298
  • 93.5k

does not contain a definition for HtmlConvertToJson

Jul 8 2016 3:14 AM
I am getting the below error which I can't find any solution in the internet :<br /><div> </div><div>&nbsp;</div><div>System.Collections.Generic.IEnumerable&lt;&gt;does not contain a definition for 'HtmlConvertToJson and no </div>extension method 'HtmlConvertToJson accepting a first argument of type 'System.Wev.Mvc.HtmlHelper&lt;System.Collections.GenericIEnumerable&lt;&gt; could<br /><div>be found(are you missing a using directive or an assembly</div><div>&nbsp;</div><div>And this is the page where the error occurs at the line <span>new ViewModel(@Html.HtmlConvertToJson(Model)));</span></div><div>&nbsp;</div><div>Hope someone can help me out on this.<br /><div class="dp-highlighter"><ol class="dp-cpp" start="1"><li class="alt"><span><span>@model&nbsp;IEnumerable&lt;BootstrapIntroduction.Models.Author&gt;&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;</span></li><li class="alt"><span>@{&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;ViewBag.Title&nbsp;=&nbsp;<span class="string">"Authors"</span><span>;&nbsp;&nbsp;</span></span></li><li class="alt"><span>}&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;</span></li><li class="alt"><span>&lt;h2&gt;Authors&lt;/h2&gt;&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;</span></li><li class="alt"><span>&lt;p&gt;&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;@Html.ActionLink(<span class="string">"Create&nbsp;New"</span><span>,&nbsp;</span><span class="string">"Create"</span><span>)&nbsp;&nbsp;</span></span></li><li class="alt"><span>&lt;/p&gt;&nbsp;&nbsp;</span></li><li class=""><span>&lt;table&nbsp;<span class="keyword">class</span><span>=</span><span class="string">"table&nbsp;table-bordered&nbsp;table-striped"</span><span>&gt;&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&lt;thead&gt;&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;tr&gt;&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;th&gt;@Html.DisplayNameFor(model&nbsp;=&gt;&nbsp;model.FirstName)&nbsp;&lt;/th&gt;&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;th&gt;@Html.DisplayNameFor(model&nbsp;=&gt;&nbsp;model.LastName)&nbsp;&nbsp;&lt;/th&gt;&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;th&gt;@Html.DisplayNameFor(model&nbsp;=&gt;&nbsp;model.Biography)&nbsp;&lt;/th&gt;&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;th&gt;Actions&lt;/th&gt;&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&lt;/tr&gt;&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&lt;/thead&gt;&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&lt;tbody&nbsp;data-bind=<span class="string">"foreach:&nbsp;authors"</span><span>&gt;&nbsp;&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&lt;tr&gt;&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;td&nbsp;data-bind=<span class="string">"text:&nbsp;FirstName"</span><span>&gt;&lt;/td&gt;&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;td&nbsp;data-bind=<span class="string">"text:&nbsp;LastName"</span><span>&gt;&lt;/td&gt;&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;td&gt;&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;a&nbsp;data-bind=<span class="string">"attr&nbsp;{href:&nbsp;'@Url.Action("</span><span>Details</span><span class="string">")/'&nbsp;+&nbsp;Id&nbsp;}"</span><span>&nbsp;</span><span class="keyword">class</span><span>=</span><span class="string">"btn&nbsp;btn-info"</span><span>&gt;Details&lt;/a&gt;&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;a&nbsp;data-bind=<span class="string">"attr&nbsp;{href:&nbsp;'@Url.Action("</span><span>Edit</span><span class="string">")/'&nbsp;+&nbsp;Id&nbsp;}"</span><span>&nbsp;</span><span class="keyword">class</span><span>=</span><span class="string">"btn&nbsp;btn-primary"</span><span>&gt;Edit&lt;/a&gt;&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;a&nbsp;data-bind=<span class="string">"attr:&nbsp;{href:&nbsp;'@Url.Action("</span><span>Delete</span><span class="string">")/'&nbsp;+&nbsp;Id&nbsp;}"</span><span>&nbsp;</span><span class="keyword">class</span><span>=</span><span class="string">"btn&nbsp;btn-danger"</span><span>&gt;Delete&lt;/a&gt;&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/td&gt;&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&lt;/tr&gt;&nbsp;&nbsp;</span></li><li class="alt"><span>&lt;/table&gt;&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;</span></li><li class="alt"><span>@section&nbsp;Scripts&nbsp;{&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&lt;script&gt;&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;ViewModel(authors)&nbsp;{&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;self&nbsp;=&nbsp;<span class="keyword">this</span><span>;&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.authors&nbsp;=&nbsp;authors;&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;};&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;viewModel&nbsp;=&nbsp;<span class="keyword">new</span><span>&nbsp;ViewModel(@Html.HtmlConvertToJson(Model)));&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ko.applyBindings(viewModel);&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&lt;/script&gt;&nbsp;&nbsp;</span></li><li class=""><span>}&nbsp; <br /></span></li></ol></div></div><div>I have added in the necessary library at the HTMLHelperExtension :</div><div>&nbsp;</div><div>&nbsp;<div class="dp-highlighter"><ol class="dp-cpp" start="1"><li class="alt"><span><span class="keyword">using</span><span>&nbsp;BootstrapIntroduction.Models;&nbsp;&nbsp;</span></span></li><li class=""><span><span class="keyword">using</span><span>&nbsp;Newtonsoft.Json;&nbsp;&nbsp;</span></span></li><li class="alt"><span><span class="keyword">using</span><span>&nbsp;System.Web;&nbsp;&nbsp;</span></span></li><li class=""><span><span class="keyword">using</span><span>&nbsp;System.Web.Mvc;&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;</span></li><li class=""><span><span class="keyword">namespace</span><span>&nbsp;BootstrapIntroduction.HtmlHelperExtension&nbsp;&nbsp;</span></span></li><li class="alt"><span>{&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">public</span><span>&nbsp;</span><span class="keyword">static</span><span>&nbsp;</span><span class="keyword">class</span><span>&nbsp;HtmlHelpherExtensions&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">public</span><span>&nbsp;</span><span class="keyword">static</span><span>&nbsp;HtmlString&nbsp;HtmlConvertToJson(</span><span class="keyword">this</span><span>&nbsp;HtmlHelper&nbsp;htmlHelper,&nbsp;object&nbsp;model)&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;settings&nbsp;=&nbsp;<span class="keyword">new</span><span>&nbsp;JsonSerializerSettings&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ReferenceLoopHandling&nbsp;=&nbsp;ReferenceLoopHandling.Ignore,&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Formatting&nbsp;=&nbsp;Formatting.Indented&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;};&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">return</span><span>&nbsp;</span><span class="keyword">new</span><span>&nbsp;HtmlString(JsonConvert.SerializeObject(model,&nbsp;settings));&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li class="alt"><span>}&nbsp; <br /></span></li></ol></div></div><div>&nbsp;</div><div>&nbsp;</div><br /><div>&nbsp;</div><div>&nbsp;</div><div>&nbsp;</div><div>&nbsp;</div><div>&nbsp;</div>

Answers (2)